I'm use reCaptcha v2 but in dev console response Uncaught (in promise) null
in in any case (and moving the .reset()
function)
console:
my code for recaptcha:
<div class="text-xs-center" style="text-align: center; height:150px;"> <p style="color: black;"> Complete the verification: </p> <div style="display: inline-block;" class="g-recaptcha" data-sitekey="xxxxxxxxxxx" data-callback="callback"></div> </div>
my callback function:
function callback() { if (grecaptcha === undefined) { alert('Recaptcha non definito'); return; } var response = grecaptcha.getResponse(); console.log(response); if (!response) { alert('Coud not get recaptcha response'); return; } $.ajax({ 'url' : 'validate-recaptcha.php', 'type' : 'POST', 'data' : { 'response' : response }, 'success' : function(data) { alert('Data: '+data); }, 'error' : function(request,error) { alert("Request: "+JSON.stringify(request)); } }); grecaptcha.reset(); }
and my validate-recaptcha.php:
<?php //debug $fp = fopen('debug.txt', 'a'); fwrite($fp, print_r($_POST, TRUE)); fclose($fp); //enddebug if (empty($_POST['recaptcha'])) { exit('Please set recaptcha variable'); } // validate recaptcha $response = $_POST['recaptcha']; $post = http_build_query( array ( 'response' => $response, 'secret' => 'yoursecretkey', 'remoteip' => $_SERVER['REMOTE_ADDR'] ) ); $opts = array('http' => array ( 'method' => 'POST', 'header' => 'application/x-www-form-urlencoded', 'content' => $post ) ); $context = stream_context_create($opts); $serverResponse = @file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context); if (!$serverResponse) { exit('Failed to validate Recaptcha'); } $result = json_decode($serverResponse); if (!$result -> success) { exit('Invalid Recaptcha'); } exit('Recaptcha Validated');
Searching on the internet, probably the problem is the .reset()
function, but I do not understand the solution.
Turns out it also occurs when a site is not "registered" in the Google recaptcha/admin Domains area.
localhost 127.0.0.1
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With