当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - recpatcha returns 200, still get error with jQuery ajax

问题描述:

All of my other searches yield responses about cross-domain requests. It appears that I'm not having a problem with that. I'm getting a response from the recaptcha server, but it's not being interpreted correctly.

<script>

function verifyCaptcha() {

jQuery.ajax({

method: "POST" ,

url: "https://www.google.com/recaptcha/api/siteverify",

data: { secret: "xxxxxxxxxxxxxxxxxxx" , response: "<% $recaptcha_response %>" },

dataType: 'jsonp' ,

success: function( result ) {

alert( result.success ) ;

},

error: function( xhr ) {

alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' Response Text: ' + xhr.responseText);

}

})

}

onload=verifyCaptcha ;

</script>

This function lives in a page that's the target of a submitted form. When I check fireBug for the results, I get this in my alert message:

Request Status: 200 Status Text: success Response Text: undefined

And the response captured by FireBug is

{

"success": true

}

It seems that it gets mad without the parens wrapped around the returned JSON which I thought were supposed to come free with dataType: 'jsonp'

How do I resolve the success of this?

UPDATE

Adding additional return parameters to the error funcion: ( xhr , message , errorThrown ) produced the following:

Request Status: 200

Status Text: success

Response Text: undefined

Message: parsererror

ErrorThrown: Error: jQuery1120043068059910713263_1455115913634 was not called

I'm guessing that the jQuery1120043068059910713263_1455115913634 was not called message is the callback function randomly named by jQuery. Does this mean that I need to add something to my logic, or that the reCaptcha server does not indeed support jsonp ?

Setting the dataType to "json" gives me a request status of 0.

UPDATE 2

I added this to my function:

jsonp: false,

jsonpCallback: 'onJSONPLoad',

and my errorThrown text changed to:

Error: onJSONPLoad was not called which leads me to conclude that reCaptcha does not support jsonp. Can anyone confirm this?

网友答案:

I guess the Answer is:

The POST method is not available for jsonp, only GET

update:

As well as in the comment stated setting the dataType to"json" and it will automatically converted to jsonp, maybe try that

also you can add a third argument to the error function

error: function( xhr, message, errorThrown ) {

and errorThrown maybe is more explicit

分享给朋友:
您可能感兴趣的文章:
随机阅读: