2

I'm trying to implement a recaptcha. I'm calling the verify script (http://www.google.com/recaptcha/api/verify) with jquery (ajax). This limits the data type to JSONP, google rejects all other data types.

the problem is that the google verify return strings like so: true success

or false 'error message'

this results in a parse error on the jquery side... Does anyone have any idea how I can resolve this or worst case scenario retrieve the responsetext even if there is a parse error?

the following is my ajax request. I added the complete and error functions for testing.

$("#verifyCaptcha").click(function(){
    var chal = Recaptcha.get_challenge();
    var resp =  Recaptcha.get_response();                   
    $.ajax({
        url: "http://www.google.com/recaptcha/api/verify",  
        type: "GET",
        data: {
            privatekey: "MyKey",
            remoteip:  "172.29.129.133", 
            challenge:  chal,
            response: resp                          
        },
        //dataType: 'jsonp',
        success: function(output){  
            output = output.split('/n');
            console.log(output[0] + '**' + output[1]);
            if(output[0].toLowerCase() == 'true')
                $("#recaptcha_result").html('Succes: form will be sent.');
            else
                $("#recaptcha_result").html('Failed: form will NOT be sent.');
        },
        complete:function (xhr, status) {
            if (status === 'error' || !xhr.responseText) 
                console.log('an error occured');
            else 
                var data = xhr.responseText;
        },
        error: function(request, status, error)
        {
         if(request.responseText == 'success')
          $("#recaptcha_result").html('Succes: form will be sent.');
         else
          $("#recaptcha_result").html('Failed: form will NOT be sent.');       
          console.log(request.responseText + '**'+status + "**"+error);       
        }
    });                     

});
Ruben Verschueren
  • 822
  • 13
  • 28

1 Answers1

4

I think its because of the type of request,its AJAX and cross domain request is invalid.

Check here fore more info:

Verifying RECAPTCHA with jQuery

Community
  • 1
  • 1
dipenrique
  • 56
  • 4