首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue.js google reCaptcha回调

Vue.js google reCaptcha回调
EN

Stack Overflow用户
提问于 2017-05-10 18:37:00
回答 8查看 23.4K关注 0票数 24

我正在尝试让recaptcha回调在组件中与vue.js一起工作。验证码本身可以工作,但不能使用我在data-callback属性中定义的回调。

我已经尝试了我能想到的所有方法,但仍然得到ReCAPTCHA couldn't find user-provided function: dothisthat错误。

这是组件

代码语言:javascript
复制
<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函数被调用。我做错了什么?

EN

回答 8

Stack Overflow用户

发布于 2017-12-17 06:20:29

我也遇到了这个问题,我花了两天的时间才解决。

因此,我将在这里为从头开始集成recaptcha和vue.js提供一个通用的答案,以便对将来遇到同样情况的人提供一个简单的指南(我假设这里使用的是vue-cli )。

注意:我在这里使用的是不可见的recaptcha,但其过程与正常的过程非常相似

第1步:

将recaptcha javascript api添加到index.html

index.html

代码语言:javascript
复制
<script src="https://www.google.com/recaptcha/api.js" async defer></script>

第2步:

创建一个名为Recaptcha的组件,或者你想叫它的任何名称(创建一个组件将使你的代码更容易阅读,更容易维护,如果你需要的话,也更容易将recaptcha添加到多个页面中)

Recaptcha.vue

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>
票数 35
EN

Stack Overflow用户

发布于 2017-05-12 05:35:23

我没有使用组件,但我也遇到了同样的问题,最后我这样解决了它:

HTML

代码语言:javascript
复制
<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

代码语言:javascript
复制
// ...
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);
    }
},
票数 15
EN

Stack Overflow用户

发布于 2019-01-17 05:26:54

如果您只查找recaptcha的响应值,以便在服务器端对其进行验证,那么一个简单的解决方案是将您的recaptcha元素放在一个表单中,并从提交event的目标元素中获取响应值。

代码语言:javascript
复制
<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方法中:

代码语言:javascript
复制
methods : {
        checkForm: function (event) {
            recaptcha_response_value = event.target['g-recaptcha-response'].value

           ...
         }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43890035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档