reCAPTCHA v3 not working angular 6 - error executing

走远了吗. 提交于 2019-12-10 20:00:33

问题


I am implementing Google reCAPTCHA v3 with Angular 6.

<script src='https://www.google.com/recaptcha/api.js?render=KEY'></script>

Added script in index.html

In my AppComponent,

constructor(
    @Inject(DOCUMENT) private document: any
) {
    this.grecaptcha = this.document.grecaptcha;
}

and when i click form submit,

this.grecaptcha.execute('KEY', { action: 'action_name' })
  .then(function (token) {
      // Verify the token on the server.
      console.log(token);
});

But,

ERROR TypeError: Cannot read property 'execute' of undefined


回答1:


the object should be available from window, so all you need is to declare it on top of your ts file:

declare const grecaptcha: any;

then you can use it in your class like:

grecaptcha.execute('KEY', { action: 'action_name' })
  .then(function (token) {
      // Verify the token on the server.
      console.log(token);
})

You can also try to install the typings @types/grecaptcha, to get some type hinting to make your life a bit easier



来源:https://stackoverflow.com/questions/53761994/recaptcha-v3-not-working-angular-6-error-executing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!