400错误时无法获取responseText
我正在研究前端网络和休息服务之间的连接。 我成功进行了跨域 Ajax 调用,但仍然遇到了一些问题。
发生400错误时无法接收responseText。
我用wireshark验证了,我确信responseText是由REST发送的。但是当我查看萤火虫时我有这个:
POST http:api.yutagz.com 用户 400 错误请求 133ms
对象{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 票证:
I'm working on the connection between a front-web and a rest service.
I succeeded to make a cross-domain Ajax call but i'm still having a little problem.
Impossible to receive the responseText when occure a 400 error.
I verified with wireshark, and i'm sure the responseText was send by the REST. But when i look in firebug i've this :
POST http: api.yutagz.com users 400 Bad Request 133ms
Object { readyState=0, status=0, statusText="error" responseText = ""}
The '400 Bad Request' is ok, but i need the responseText to say the user what really happen.
Here is my call code (working on success event) :
$.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.
}
}
});
Here is a test (working with Chrome but not with firefox 3.6):
And here a jQuery ticket:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 Chrome 13 中看到:
在 FF6 中:
所以只需使用
error.message
并从那里开始。I see in Chrome 13:
And in FF6:
So just use
error.message
and go from there.