我正在尝试让recaptcha回调在组件中与vue.js一起工作。验证码本身可以工作,但不能使用我在data-callback属性中定义的回调。
我已经尝试了我能想到的所有方法,但仍然得到ReCAPTCHA couldn't find user-provided function: dothisthat错误。
这是组件
<script>
function dothisthat (){
alert(312);
}
</script>
<template>
<div class="well main-well">
<h4>Captcha</h4>
<p class="small">You must complete the captcha to finish your booking.</p>
<div id="captcha-wrapper">
<div class="g-recaptcha" :data-sitekey="captchaKey" data-callback="dothisthat"></div>
</div>
</div>
</template>
<script>
function dothisthat (){
alert(123);
}
import * as filters from '../../../filters';
import Translation from '../../../Translation';
export default {
name: 'Captcha',
props: {
},
computed: {
captchaKey: function() {
return this.$store.getters.captcha;
}
},
methods: {
dothisthat: function(){
return function() {
console.log("123");
};
}
},
mounted(){
function dothisthat() {
alert(123);
}
$(function() {
function dothisthat() {
alert(123);
}
});
}
}
</script>没有一个dothisthat函数被调用。我做错了什么?
发布于 2017-12-17 06:20:29
我也遇到了这个问题,我花了两天的时间才解决。
因此,我将在这里为从头开始集成recaptcha和vue.js提供一个通用的答案,以便对将来遇到同样情况的人提供一个简单的指南(我假设这里使用的是vue-cli )。
注意:我在这里使用的是不可见的recaptcha,但其过程与正常的过程非常相似
第1步:
将recaptcha javascript api添加到index.html
index.html
<script src="https://www.google.com/recaptcha/api.js" async defer></script>第2步:
创建一个名为Recaptcha的组件,或者你想叫它的任何名称(创建一个组件将使你的代码更容易阅读,更容易维护,如果你需要的话,也更容易将recaptcha添加到多个页面中)
Recaptcha.vue
<template>
<div
id="g-recaptcha"
class="g-recaptcha"
:data-sitekey="sitekey">
</div>
</template>
<script>
export default {
data () {
return {
sitekey: '6LfAEj0UAAAAAFTGLqGozrRD8ayOy*********',
widgetId: 0
}
},
methods: {
execute () {
window.grecaptcha.execute(this.widgetId)
},
reset () {
window.grecaptcha.reset(this.widgetId)
},
render () {
if (window.grecaptcha) {
this.widgetId = window.grecaptcha.render('g-recaptcha', {
sitekey: this.sitekey,
size: 'invisible',
// the callback executed when the user solve the recaptcha
callback: (response) => {
// emit an event called verify with the response as payload
this.$emit('verify', response)
// reset the recaptcha widget so you can execute it again
this.reset()
}
})
}
}
},
mounted () {
// render the recaptcha widget when the component is mounted
this.render()
}
}
</script>第3步:
导入recaptcha组件并将其添加到您的页面(父组件)。
page.vue
<template>
<div>
<h1>Parent component (your page)</h1>
<button @click="executeRecaptcha">execute recaptcha</button>
<!-- listen to verify event emited by the recaptcha component -->
<recaptcha ref="recaptcha" @verify="submit"></recaptcha>
</div>
</template>
<script>
import Recaptcha from 'recaptcha'
export default {
components: {
Recaptcha
},
methods: {
// send your recaptcha token to the server to verify it
submit (response) {
console.log(response)
},
// execute the recaptcha widget
executeRecaptcha () {
this.$refs.recaptcha.execute()
}
}
}
</script>发布于 2017-05-12 05:35:23
我没有使用组件,但我也遇到了同样的问题,最后我这样解决了它:
HTML
<div id="recaptcha" class="g-recaptcha"></div>
<button id="submit" @click="validate">Submit</button>
<script src="https://www.google.com/recaptcha/api.js?render=explicit" async defer></script>JS
// ...
mounted: function() {
this.initReCaptcha();
},
methods: {
initReCaptcha: function() {
var self = this;
setTimeout(function() {
if(typeof grecaptcha === 'undefined') {
self.initReCaptcha();
}
else {
grecaptcha.render('recaptcha', {
sitekey: 'SITE_KEY',
size: 'invisible',
badge: 'inline',
callback: self.submit
});
}
}, 100);
},
validate: function() {
// your validations...
// ...
grecaptcha.execute();
},
submit: function(token) {
console.log(token);
}
},发布于 2019-01-17 05:26:54
如果您只查找recaptcha的响应值,以便在服务器端对其进行验证,那么一个简单的解决方案是将您的recaptcha元素放在一个表单中,并从提交event的目标元素中获取响应值。
<form class="container"
@submit="checkForm"
method="post"
>
... // other elements of your form
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<p>
<input class="button" type="submit" value="Submit">
</p>
</form>在checkForm方法中:
methods : {
checkForm: function (event) {
recaptcha_response_value = event.target['g-recaptcha-response'].value
...
}https://stackoverflow.com/questions/43890035
复制相似问题