0

I'm using ajaxSetup to handle all errors generally:

$.ajaxSetup({
    error: handleErrors
});

var handleErrors = function(xhr) {
    console.log('this is ');
    console.log(this);
};        

In this case, this is the global scope.

However, if I have an indiviudal error handler for each call:

$.ajax({
    error: function(xhr){
        console.log('this is ');
        console.log(this);
    },  
    ...

The this is the ajax request that was sent, with url, type, contentType etc. How would I get access to that information in my general handleErrors function? So if it fails, I know specifically the request that has failed?

carloabelli
  • 4,289
  • 3
  • 43
  • 70
Mark
  • 4,428
  • 14
  • 60
  • 116
  • have you checked dis "http://stackoverflow.com/questions/19405033/get-server-response-with-ajax-error" – Light Jul 28 '15 at 10:43
  • I know how to get the error, i want to get the original request from a general error handling function.... – Mark Jul 28 '15 at 10:50

2 Answers2

1

function(data, textStatus, jqXHR){ }

Vivek Ranjan
  • 1,432
  • 2
  • 15
  • 37
0

JQuery documentation advises to use ajaxError for this.

You can attach a function handler to it with the following options:

handler

Type: Function( Event event, jqXHR jqXHR, PlainObject ajaxSettings, String thrownError )

jqXHR will be your XHR object. While the thrownError will be the error message.

As of jQuery 1.8 it's mandatory to attach this function to the document only.

Mouser
  • 13,132
  • 3
  • 28
  • 54