简体   繁体   English

Angular recaptcha v3,代码显示错误,无法执行该功能

[英]Angular recaptcha v3, code is showing error, not able to execute the function

I am using recaptcha v3 with Angular and facing executing error.我正在使用带有 Angular 的 recaptcha v3 并面临执行错误。

click(){
  this.grecaptcha.execute();
  this.grecaptcha.ready('6LfwjpEUAAAAAHTtLNdC42zZZ64LM8nPqXf47vuZ', { action: 'stackblitz' })
  .then(function (token) {
      // Verify the token on the server.
      console.log(token);
  });
}

please check console of my stackblitz link请检查我的 stackblitz 链接的控制台

stackblitz 闪电战

I used below stack question but it didn't help me out我使用了下面的堆栈问题,但它没有帮助我

reCAPTCHA v3 not working angular 6 - error executing . reCAPTCHA v3 不工作 angular 6 - 执行错误


UPDATE:更新:

I am getting the error, ERROR Error: Cannot read property 'execute' of undefined.我收到错误,错误错误:无法读取未定义的属性“执行”。

in HTML I tried在 HTML 我试过

<script src="https://www.google.com/recaptcha/api.js?render=6LfwjpEUAAAAAHTtLNdC42zZZ64LM8nPqXf47vuZ"></script>

in ts , on submit I am calling在 ts ,提交时我打电话

declare const grecaptcha: any;
onsubmit(){
    this.grecaptcha.ready(() => {
     {
        this.grecaptcha.execute('6LfwjpEUAAAAAHTtLNdC42zZZ64LM8nPqXf47vuZ', {action: 'information'}).then(function(token) {
           console.log(token);
        });
     }

}) } }) }

First note, you should post your code, since links tend to die.首先请注意,您应该发布您的代码,因为链接往往会失效。 Secondly, when checking the documentation , it states that this is how it is called:其次,在检查文档时,它指出这是如何调用的:

<script>
  grecaptcha.ready(function() {
    grecaptcha.execute('site_key', {action: 'homepage'}).then(function(token) {
       // ...
    });
  });
</script>

So you should first call grecaptcha.ready , before execute , so, like in the question you linked, declare grecaptcha outside the component and call the function in your component:所以你应该先调用grecaptcha.ready ,然后再execute ,所以,就像你链接的问题一样,在组件外声明grecaptcha并在你的组件中调用函数:

declare const grecaptcha: any;

// ...

grecaptcha.ready(() => {
  grecaptcha.execute('sitekey', { action: 'test' }).then((token) => {
    console.log(token);
  });
});

Also when looking at the grecaptcha, seems you are using some kind of test key, in v 3 you need to create your own key and add the domain in contrary with v 2 where you could try a test key provided by google.此外,在查看 grecaptcha 时,似乎您正在使用某种测试密钥,在 v 3 中,您需要创建自己的密钥并添加与 v 2 相反的域,您可以在其中尝试使用谷歌提供的测试密钥。

If you are developing on localhost, add http://127.0.0.1/ as domain when you register to get your keys.如果您在 localhost 上开发,请在注册以获取密钥时添加http://127.0.0.1/作为域。 Because of this restriction, I cannot "fix" the stackblitz you provided.由于此限制,我无法“修复”您提供的 stackblitz。

I tried the above code in a test project, and it worked wonderfully for me :)我在一个测试项目中尝试了上面的代码,它对我来说效果很好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM