How can I bubble an error from an $.ajax instance to the $.ajaxSetup?

133 Views Asked by At

I have an error callback defined in ajax setup, that will be executed in each ajax instance:

$.ajaxSetup({

    cache: false,
    error: function (data, textStatus, pStatusDescription) {

        if (pStatusDescription === "Unauthorized")
        {
            // show dialog
            alert("Your Login has expired. Please re-login.");
            return;
        }
    }
});

Also I have a simple ajax request which has an own definition for the error handling. This definition overwrites the definition in $.ajaxSetup:

$.ajax({
    url: "http://hostname",
    success: function(xhr) { ... },
    error: function(data, textStatus, pStatusDescription) { 

        if(pStatusDescription === "ObjectNotFound")
        {
            // remove Object
            return;
        }

        // call 'error' callback in $.ajaxSetup now.
    }
});

How can I bubble an error from an $.ajax instance to the $.ajaxSetup?

1

There are 1 best solutions below

1
Daydreaming Duck On

I think issue is: you are using ajaxSetup wrong way. Check the note in ajaxSetup docs:

Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().

So if you ajaxError instead of ajaxSetup, you can catch error inside ajaxError handler for global handler, and add additional handler for particular request inside 'error' parameter of $.ajax