400错误时无法获得responseText

时间:2011-09-15 17:07:43

标签: jquery ajax responsetext

我正在研究前端网络和休息服务之间的连接。 我成功地进行了跨域Ajax调用,但我仍然遇到了一些问题。

发生400错误时无法收到responseText。

我使用wireshark验证,我确定responseText是由REST发送的。但当我看到萤火虫时,我就是这样:

  

POST http:api.yutagz.com用户400 Bad Request 133ms

     

对象{readyState = 0,status = 0,statusText =“error”responseText =“”}

'400错误请求'没问题,但我需要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):

http://jsfiddle.net/RTvQQ/

这是一张jQuery票证:

http://bugs.jquery.com/ticket/7868

1 个答案:

答案 0 :(得分:1)

我在Chrome 13中看到了:

在FF6中:

所以只需使用error.message并从那里开始。