我能够检测到成功的响应,但无法检测到错误。我的代码:如何检测jQuery ajax错误响应?
$.ajax({
url: url,
dataType: 'jsonp',
success: function(data){
$.each(data, function(a, b) {
if(b=="Processing post"){
$("#post_response").html('Your comment will be added soon...');
}
else if(b=="In moderation"){
$("#post_response").html('Your comment has been placed in moderation...');
}
})
},
error: function(xhr, testStatus, error) {
alert("error");
}
});
这些是我收到的回复。我可以检测到前2个响应,但我甚至无法提醒最后一个“错误”。
jQuery191004289518775843171_1361906819321({"Message":"Processing post","Code":202})
jQuery191006158838421090984_1361907415719({"Message":"In moderation","Code":202})
jQuery191006158838421090984_1361907415719({"Message":"Not logged in","Code":403})
你能看到我要去哪里吗?
您无法使用JSONP捕获请求错误。如果服务器除了jsonp之外还返回403的文本状态,jQuery将无法处理它。 – 2013-02-26 19:47:56
你是否断言在第三种情况下“成功”没有被调用?尝试添加一个'console.log'或'alert'。 – pdoherty926 2013-02-26 19:48:06
我试着添加其他if(b ==“Not logged in”){$(“#post_response”)。html('您没有登录'); \t}成功,但没有拿起。 – 2013-02-26 20:07:19