我正在处理前端网页和REST服务之间的连接。我成功地进行了跨域Ajax调用,但仍然有一些小问题。
在发生400错误时无法接收responseText。
我使用Wireshark进行验证,确信REST已发送responseText。但是当我看Firebug时,我得到以下信息:
POST http://api.yutagz.com/users 400 Bad Request 133ms Object { readyState=0, status=0, statusText="error" responseText = ""}
“400 Bad Request”没有问题,但我需要responseText来告诉用户真正发生了什么。
以下是我的调用代码(在成功事件上工作):
在发生400错误时无法接收responseText。
我使用Wireshark进行验证,确信REST已发送responseText。但是当我看Firebug时,我得到以下信息:
POST http://api.yutagz.com/users 400 Bad Request 133ms Object { readyState=0, status=0, statusText="error" responseText = ""}
“400 Bad Request”没有问题,但我需要responseText来告诉用户真正发生了什么。
以下是我的调用代码(在成功事件上工作):
$.ajax({
type: "POST",
url : "http://api.yutagz.com/users",
data: dataString,
dataType: 'json',
success : function(data,data1,data2){
alert("OK : "+data);
console.log(data2);
},
error:function (xhr){
alert(JSON.stringify(xhr));
console.log(xhr);
switch (xhr.status) {
case 404: alert("404");
case 400: alert("400");
// Take action, referencing xhr.responseText as needed.
}
},
complete : function (xhr){
alert(JSON.stringify(xhr));
console.log(xhr);
switch (xhr.status) {
case 404: alert("404");
case 400: alert("400");
// Take action, referencing xhr.responseText as needed.
}
}
});
这是一个测试(在Chrome中可以正常工作,但在Firefox 3.6中不能):
以下是jQuery的一个问题报告: