1

When do a jQuery.ajax call, and it fails, the function assigned to the error option provides very sparse error information, yet when I use Chrome developer tools, the network tab, I see the response includes an HTML document with a much more verbose and informative explanation of the error.

Can I get this kind of error information back from the $.ajax call, and if so, how?

ProfK
  • 49,207
  • 121
  • 399
  • 775

4 Answers4

0

You can use the jqXHR responseText property, it will use the browser's native XMLHttpRequest object and you can get the full response body.

$.ajax(url).fail(function(jqXHR, status, errorThrown) {
    console.log(jqXHR.responseText);
    console.log(status);
    console.log(errorThrown);
})

http://api.jquery.com/jquery.ajax/#jqXHR

Sofiane Sadi
  • 357
  • 1
  • 7
0

The error callback in jQuery gives the XMLHttpRequest object as the first parameter.

Use xhr.response or xhr.responseText to get the full error page.

$.ajax({
    error: function(xhr, status, error) {
        xhr.response;
    }
});

For information on how to parse the response, see What is the best practice for parsing remote content with jQuery?

Community
  • 1
  • 1
4castle
  • 32,613
  • 11
  • 69
  • 106
0

Are you looking for something like this?

$.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
}).fail(function (jqXHR, textStatus) {
    document.body.innerHTML = jqXHR.responseText;
});

https://plnkr.co/edit/Aj7Tgxxno9fCcYBlwKuJ?p=preview

samir benzenine
  • 478
  • 4
  • 11
-2
   var printError = function( req, status, err ) {
  console.log( 'something went wrong', status, err );
};

// Create an object to describe the AJAX request
var ajaxOptions = {
  url: '/data/people.json',
  dataType: 'json',
  success: your function,
  error: printError
};

I think this is a better approach.

vbotio
  • 1,566
  • 3
  • 26
  • 53
  • It's the bad solution I'm trying to improve on. It hardly gives any info, where the HTTP response to the `$.ajax` call includes a nice and verbose explanation of the error and even possible causes. That is the whole purpose of my question: how to get the info in that HTML document. – ProfK May 20 '16 at 17:35