Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google reCaptcha response "Uncaught (in promise) null"

I'm use reCaptcha v2 but in dev console response Uncaught (in promise) null in in any case (and moving the .reset() function)

console:

enter image description here

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.

like image 727
FABBRj Avatar asked Sep 18 '18 16:09

FABBRj


Video Answer


2 Answers

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:

  1. Sign into your Google account where your recaptcha keys are registered
  2. Type into Google "google recpatcha admin console"
  3. Go to your settings for your (production) key
  4. In "Domains", add these two entries:
localhost 127.0.0.1 
  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.

like image 87
EliteRaceElephant Avatar answered Oct 08 '22 04:10

EliteRaceElephant


Recaptcha v2 callback js error

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 :)

like image 20
EsaulFarfan Avatar answered Oct 08 '22 03:10

EsaulFarfan