Solved: Google reCAPTCHA always returns “false” as a response to AJAX call

I have explained how to use Google reCAPTCHA in another post. Google returns “true” or “false” in “success” parameter as a response to your AJAX call.

After your implementation, you might be constantly getting “false” value in this parameter. The response looks like this in your browser trace:

{
“success”: false,
“error-codes”: [
     “missing-input-response”,
     “missing-input-secret”
      ]
}

Issue

You might be sending “secret” and “response” parameters as hash map. Your code may look like this:

$.ajax({
     type: "POST",
     url: "https://www.google.com/recaptcha/api/siteverify",
     data: {
     secret: "YOUR-SECRET-KEY",
     response: captchaResult
},
contentType: "application/json; charset=utf-8",
dataType: "json",
failure: function (response) {
     alert(response.d);
},
success: function (response) {
     googleCallResult = response.success;
}
}).done(function () {
     // Do stuff with googleCallResult
});

Solution

Google reCAPTCHA expects parameters to be sent in the URL instead of hash map. So you need to add “secret” and “response” parameters into the URL. Here is the edited code:

$.ajax({
     type: "POST",
     url: "https://www.google.com/recaptcha/api/siteverify?secret=YOUR-SITE-KEY&response=" + captchaResult,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     failure: function (response) {
     alert(response.d);
},
success: function (response) {
     googleCallResult = response.success;
}
}).done(function () {
     // Do stuff with googleCallResult
});

One thought to “Solved: Google reCAPTCHA always returns “false” as a response to AJAX call”

Leave a Reply