impossible to get the responseText when 400 error
Asked Answered
T

1

7

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):

http://jsfiddle.net/RTvQQ/

And here a jQuery ticket:

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

Toft answered 15/9, 2011 at 17:7 Comment(2)
In Chrome, this is working fine for me. I get: "{"error":{"type":"badRequest","message":"errorUserEmailMissing"}}"Scagliola
It's working with Firefox 5, i just tested. But what's going on with 3.6Toft
T
1

I see in Chrome 13:

And in FF6:

So just use error.message and go from there.

Tunicate answered 15/9, 2011 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.