javascript - grecaptcha.execute() calls data-callback before user challenge - Stack Overflow

I want to invoke programmatically the invisible reCaptcha challenge.According to reCaptcha document, I

I want to invoke programmatically the invisible reCaptcha challenge. According to reCaptcha document, I have written the following code:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>reCaptcha test</title>
    <script>
        function onReCaptchaSubmit(token) {
            alert("reCaptcha challenge response:\n" + token);
        }
    </script>
    <script src=".js" async defer></script>
    <style>
        .grecaptcha-badge {
            display: none;
        }
    </style>
</head>

<body>

<a href="javascript:void(0);" onclick="grecaptcha.execute();">Challenge</a>

<div class="g-recaptcha"
     data-sitekey="*******"
     data-callback="onReCaptchaSubmit"
     data-size="invisible">
</div>

</body>
</html>

and grecaptcha.execute(); is called from an HTML link to display reCaptcha challenge popup. In most cases, It works well. but sometimes, after calling grecaptcha.execute(); , It calls back onReCaptchaSubmit(), without displaying reCapcha popup and user challenge.

How can I fix it?
Thanks in advance.

I want to invoke programmatically the invisible reCaptcha challenge. According to reCaptcha document, I have written the following code:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>reCaptcha test</title>
    <script>
        function onReCaptchaSubmit(token) {
            alert("reCaptcha challenge response:\n" + token);
        }
    </script>
    <script src="https://www.google./recaptcha/api.js" async defer></script>
    <style>
        .grecaptcha-badge {
            display: none;
        }
    </style>
</head>

<body>

<a href="javascript:void(0);" onclick="grecaptcha.execute();">Challenge</a>

<div class="g-recaptcha"
     data-sitekey="*******"
     data-callback="onReCaptchaSubmit"
     data-size="invisible">
</div>

</body>
</html>

and grecaptcha.execute(); is called from an HTML link to display reCaptcha challenge popup. In most cases, It works well. but sometimes, after calling grecaptcha.execute(); , It calls back onReCaptchaSubmit(), without displaying reCapcha popup and user challenge.

How can I fix it?
Thanks in advance.

Share Improve this question edited Aug 7, 2017 at 15:41 Askar asked Aug 7, 2017 at 11:12 AskarAskar 5741 gold badge6 silver badges18 bronze badges 2
  • 1 You may not want to use your real sitekey. – colecmc Commented Aug 7, 2017 at 15:32
  • @colecmc, Thanks, I removed it. – Askar Commented Aug 7, 2017 at 15:44
Add a ment  | 

2 Answers 2

Reset to default 3

You expect invisible reCaptcha to show user challenge every time you call grecaptcha.execute(), but it does not work that way. In my experience, it shows challenge only when it has doubts about you. If Google is sure that you are human, it skips displaying of user challenge and proceeds to callback.

Since recaptcha has an expiration time, you have to wait until your recaptcha session expires or call grecaptcha.reset() inside onRecaptchaSubmit to ensure user challenge appear everytime user clicks the link:

function onRecaptchaSubmit(token) {
  /* any behavior you want */
  //...
  window.grecaptcha.reset();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743723093a4496156.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信