PHP is not returning data to AJAX to validate Captcha

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>
    
asked by anonymous 05.11.2017 / 12:32

1 answer

0

For the message you returned, it should 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 to add this host too and use this new key.

    
05.11.2017 / 15:44