7

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

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

我用wireshark验证了,我确定responseText是由REST发送的。但是当我看萤火虫时,我有这个:

POST http: api.yutagz.com users 400 Bad Request 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):

http://jsfiddle.net/RTvQQ/

这里有一张 jQuery 票:

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

4

1 回答 1

1

我在 Chrome 13 中看到:

在FF6中:

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

于 2011-09-15T17:17:26.100 回答