recpatcha返回200,jQuery ajax仍然出错

时间:2016-02-09 16:35:12

标签: javascript jquery ajax recaptcha

我的所有其他搜索都会产生有关跨域请求的响应。看来我没有遇到问题。我从recaptcha服务器得到了回复,但它没有被正确解释。

<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>

此功能位于提交表单的目标页面中。当我检查fireBug的结果时,我会在警报消息中看到:

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

FireBug捕获的响应是

{
   "success": true
}

看起来如果没有围绕返回的JSON的parens就会生气,我认为它应该是免费的dataType: 'jsonp'

如何解决此问题的成功?

更新

向错误函数添加其他返回参数:( xhr , message , errorThrown )产生以下内容:

Request Status: 200
Status Text: success
Response Text: undefined
Message: parsererror
ErrorThrown: Error: jQuery1120043068059910713263_1455115913634 was not called

我猜测jQuery1120043068059910713263_1455115913634 was not called消息是jQuery随机命名的回调函数。这是否意味着我需要在逻辑中添加一些东西,或者reCaptcha服务器确实不支持jsonp?

dataType设置为"json"会使我的请求状态为0。

更新2

我将此添加到我的功能中:

jsonp: false,
jsonpCallback: 'onJSONPLoad',

我的errorThrown文字更改为: Error: onJSONPLoad was not called让我得出结论,reCaptcha不支持jsonp。谁能证实这一点?

1 个答案:

答案 0 :(得分:0)

我猜答案是:

POST方法不适用于jsonp,只有GET

更新

在评论声明中将dataType设置为"json"并且它会自动转换为jsonp,也许会尝试

你也可以在错误函数

中添加第三个参数
error: function( xhr, message, errorThrown ) {

和errorThrown可能更明确

相关问题