Question

I'm looking to re use the success/done function that was used for a jquery ajax call inside of it's error/fail function.

The scenario I need to handle is when a user tries to do something their account doesn't have permission to do the web server returns with an error, all of the errors are run through the same error handling function which has a special case for permission errors. When the user tries to access something that they don't have permission to access/modify it creates a popup where they can enter login information and run their previous request over again, I have the previous request stored so all of the information they sent is stored. The problem I'm running into is sending the request over again and making sure the response gets routed back to the appropriate callback functions.

The error function has access to the jqXHR object associated with the request and I was wondering if it's possible to take the callback functions associated with that jqXHR and attach them to the new request.

Was it helpful?

Solution

I've never tried this, but in theory it should work in jQuery 1.5+ considering this is a reference to the passed in options if you don't set a context.

$.ajax({
    ... ajax options ...
    // context: el <--- DO NOT USE THIS OPTION    
    error: function() {
        $.ajax(this);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top