tags:

views:

156

answers:

1

jQuery's AJAX error function has the following parameters:

error(XMLHttpRequest, textStatus, errorThrown)

What's the best cross-browser way to get the response body?

Does this work (reliably in all browsers)?

$.ajax({
  error: function(http) {
    alert(http.responseText);
  }
});
+4  A: 

There is a hidden function that can extract the data from XHR istance:

var responseText = $.httpData(xhr)

If you pass "json" as a second parameter it will treat the response as a JSON string.

Note that you might get an error because there is no response (network problem for example). Make sure you cover that case as well. Also, I believe (not sure) that jQuery invokes the error handler if the server returns a 4xx or 5xx status.

Emil Ivanov
Thanks. Judging from the source it looks like `xhr.responseText` will work for regular HTML data
Horace Loeb