Google reCaptcha response "Uncaught (in promise) null"
I had this error too and I found is related with the recaptcha callback (in your case data-callback="callback"
). If you remove your data-callback attribute the error won't come up.
The console error Uncaught (in promise) null
indicates the callback is waiting for a promise. Here's a basic callback function for recaptcha using promises:
function callback() {
return new Promise(function(resolve, reject) {
//Your code logic goes here
//Instead of using 'return false', use reject()
//Instead of using 'return' / 'return true', use resolve()
resolve();
}); //end promise
};
In your case you need to adjust your code to something like this:
function callback() {
return new Promise(function(resolve, reject) {
if (grecaptcha === undefined) {
alert('Recaptcha non definito');
//return;
reject();
}
var response = grecaptcha.getResponse();
console.log(response);
if (!response) {
alert('Coud not get recaptcha response');
//return;
reject();
}
$.ajax({
'url' : 'validate-recaptcha.php',
'type' : 'POST',
'data' : {
'response' : response
},
'success' : function(data) {
alert('Data: '+data);
resolve();
},
'error' : function(request,error)
{
alert("Request: "+JSON.stringify(request));
reject();
}
});
grecaptcha.reset();
}); //end promise
}
This is my first answer in SO, so please be patient and let me know if I forgot or missed something :)
Turns out it also occurs when a site is not "registered" in the Google recaptcha/admin Domains area.
Solution: Add the domain in the recaptcha admin area:
- Sign into your Google account where your recaptcha keys are registered
- Type into Google "google recpatcha admin console"
- Go to your settings for your (production) key
- In "Domains", add these two entries:
localhost 127.0.0.1
- Save it and test your recaptcha.
I made this error when I switched from a development key to a production key. The production key did not have any entries for localhost.
I configured the API response to sit behind a proxy-redirect. Therefore the verification was working in a localhost environment which was not configured in the Google Admin console which caused this generic error.
Credit to @Christian Žagarskas who pointed it out in his comment.