【发布时间】:2018-04-11 16:31:47
【问题描述】:
我有一个关于在使用 Vue 组件提交 AJAX 表单时使用 Google 的 Invisible Recaptcha 的问题。
我创建了一个 VueJS 组件,我将其包含在以下“recaptcha button”组件中:
<template>
<div>
<div v-if="failed" style="color: red;"><strong>Sorry, the captcha validation failed. Please try again.</strong></div>
<div :id="name"></div>
<button :class="classes" type="button" @click="validate()">
<slot>Submit</slot>
</button>
</div>
</template>
<script>
export default {
props: {
name: {
type: String,
default: 'recaptcha',
required: false
},
classes: {
type: String,
required: false,
default: ''
},
},
data: function ()
{
return {
failed: false,
};
},
mounted: function ()
{
this.initReCaptcha();
},
methods: {
initReCaptcha: function() {
var self = this;
setTimeout(function() {
if(typeof grecaptcha === 'undefined') {
self.initReCaptcha();
}
else {
grecaptcha.render(self.name, {
sitekey: 'site-key-here',
size: 'invisible',
badge: 'inline',
callback: self.response
});
}
}, 100);
},
validate: function ()
{
grecaptcha.execute();
},
response: function (token)
{
this.$parent.fields['g-recaptcha-response'] = token;
this.$parent.submit();
}
},
}
</script>
如您所见,我将 Google 的 Recaptcha 的回调函数分配给 recaptcha 组件的“响应”函数的当前实例。但是,当我提交其中一个表单时,它似乎正在调用页面上其他组件的响应函数..因此试图在迄今为止没有输入的表单上调用提交..
我们认为这可能是 recaptcha 渲染的一种情况,实际上并没有创建两个实例,因此在第二个实例中,回调只是被覆盖,而是通过将组件记录在挂载函数中,提交的表单而不是我们尝试提交的那个是首先被实例化的,这让我们相信这不是覆盖的情况......
任何关于此事的帮助将不胜感激!
干杯, 下午
【问题讨论】: