Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery get data on XHR error

If I make a jquery AJAX request which is succesful I get back my JSON data. However, If I make a request and I get somthing other than a 200 response code back, I cannot get back the data in the Jquery call back. I need the data as it has a description about the data.

success: function (data, tst, xhr) {
    $.log('XHR OK');
},
error: function (xhr, tst, err) {
    $.log('XHR ERROR ' + XMLHttpRequest.status);
},

Any ideas?

Thanks

like image 247
James Moore Avatar asked Oct 20 '10 13:10

James Moore


1 Answers

In the:

error: function (xhr, tst, err) {
    $.log('XHR ERROR ' + XMLHttpRequest.status);
},

you can use

error: function (XMLHttpRequest, textStatus, errorThrown) {
    $.log('XHR ERROR ' + XMLHttpRequest.status);
    return JSON.parse(XMLHttpRequest.responseText);
},

to get the JSON response in in event of an error.

XMLHttpRequest.responseText

Cheers.

like image 134
James Moore Avatar answered Oct 14 '22 19:10

James Moore