Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google reCaptcha 2 TypeError: a is null after calling grecaptcha.reset

I have an AJAX based registration page which validates on both the client and the server.

When the server side validation fails the AJAX returns the errors to the screen and attempts to reset the recaptcha using grecaptcha.reset().

Resetting the recaptcha appears to work fine and the customer is able to retick the "I'm not a robot" box and carry on, however after the recaptcha has been reset and scroll action on the page causes a massive number of TypeError: a is null javascript errors in recaptcha_en.js.

TypeError: a is null
https://www.gstatic.com/recaptcha/api2/r20151104115408/recaptcha__en.js
Line 50

Code (simplified):

var recaptcha1;
var onRecaptchaLoad = function () {
    recaptcha1 = grecaptcha.render('gRecaptcha', {
        'sitekey': 'my site key',
        'callback': CaptchaResponse
    });
};

$.ajax({
type: "POST",
url: "SaveDetails",
contentType: "application/json; charset=utf-8", 
dataType: "json",
data: data,
cache: false,
success: function (result) {
    if (result != null) {
        if (result.d.Success) {
            //success
        }else{             
            grecaptcha.reset(recaptcha1);
        }
     }
}
});
like image 298
Morgeh Avatar asked Nov 09 '15 16:11

Morgeh


People also ask

How do I reset reCaptcha v2?

For reCaptcha v2, use: grecaptcha. reset();

How do I extend reCaptcha timeout?

One solution is to change the way you are submitting your form. Move the reCaptcha submission to a secondary server side page if possible. This will create a fresh condition where the interaction with Google is immediate.


1 Answers

Have a same problem and everything I found useful to think about is that recapthca is trying to load old version or something (I can not reproduce but I got another version of captcha under certain circumstances)... and another thought related to modal window where my recaptcha show same behavior.

BTW: not only scroll event trigger errors spamming but any window re-sizing as well.

I hope any of this can help.

like image 106
Monopompom Avatar answered Oct 03 '22 08:10

Monopompom