PHP is not returning data to AJAX to Validate Captcha

Asked

Viewed 63 times

1

JAVASCRIPT

function validateCaptcha(){
    var responsec = grecaptcha.getResponse();

    if(responsec.length == 0){

    }else{
        $.ajax({
                  type: "POST",
                  url: "kingSecure/server.php",
                  async: false,
                  data: {
                    "captcha": responsec
                  },
                  success: function(resp) {
                        if(resp == "success") {
                            alert("success");
                        }
                        else {
                            alert("fail");
                        }
                  }
                });
    }
        }

PHP

$response = $_POST['captcha'];

$privatekey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 
$param = "https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$response;
$verifyResponse = file_get_contents($param);
$responseData = json_decode($verifyResponse);

    if($responseData->success){
    echo  'success';
    }

HTML

<div class="g-recaptcha" data-sitekey="6Len9TYUAAAAAGmBB_lYT7NDXfWlTxWHCs11cWZ8" data-callback="validateCaptcha"></div>
  • What appears from code debugging in the browser?

  • Nothing appears. On the Chrome network appears that the POST is OK, IE, was sent correctly.

  • Try to print the variable dump $responseData. Something like var_dump($responseData), just don’t put inside the if.

  • object(stdClass)[1]&#xA; public 'success' => boolean false&#xA; public 'challenge_ts' => string '2017-11-05T13:28:45Z' (length=20)&#xA; public 'hostname' => string '127.0.0.1' (length=9)&#xA; public 'error-codes' => &#xA; array (size=1)&#xA; 0 => string 'invalid-keys' (length=12)

  • I think the code is breaking, or the same expires until I send the request to it.

1 answer

0

For the message that returned, it must be that in the Google Recaptcha Admin you do not have the host that is sending the request, in the case of localhost/127.0.0.1, registered and with a key. Try adding this host as well and using this new key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.