3

I'm trying to implement a jQuery default HTTP error handler for AJAX calls. I've implemented the default error handler for generic application errors as follows:

$(document).ajaxError(function(event, jqXHR, settings, thrownError) {
    switch (jqXHR.status) {
        case 403:
            console.log('Forbidden, go away!');
            break;
        case 404:
            console.log('Resource not found :O');
            break;
        case 422:
            console.log('Unprocessable entity.');
            break;
        case 500:
            console.log('Someone have to pay for this!');
            break;
        default:
            console.log('Anyone knows what is going on here?');
    }
});

Now what i would like to achieve is to override a specific status code within a single AJAX call. Like for example

$.ajax({
    ...
    error: function(jqXHR, textStatus, errorThrown) {
         if(jqXHR.status === 404){
             console.log('The requested unicorn is actually missing');
         }
    }
})

Right now, if i implement as shown above, both messages will be displayed

>The requested unicorn is actually missing
>Resource not found :O

while i'm trying only to get The requested unicorn is actually missing message.

Setting global: false flag in the single AJAX call settings implies ignoring all global AJAX functions within that AJAX call and unfortunately that is not a viable solution for my application.

Any idea? Thanks!

2
  • jsfiddle.net/07wzfyyL global options should have worked, check out this fiddle. Commented Mar 6, 2017 at 11:32
  • From the jQuery docs: global: Whether to trigger global Ajax event handlers for this request. The default is true. Set to false to prevent the global handlers like ajaxStart or ajaxStop from being triggered. This can be used to control various Ajax Events. This behavior prevent global handlers execution in any case! What i need here is to override only a specific status code if handled by the single ajax call and execute the default handler only if the HTTP code is not handled through the application. Commented Mar 6, 2017 at 11:41

1 Answer 1

5

you can do the following

$.ajax({
    handleErrors: [404],
    ...
    error: function(jqXHR, textStatus, errorThrown) {
        if(jqXHR.status === 404){
            console.log('The requested unicorn is actually missing');
        }
    }
});

then in your global handler check if the error is handled

$(document).ajaxError(function(event, jqXHR, settings, thrownError) {
    if (settings.handleErrors && settings.handleErrors.indexOf(jqXHR.status) !== -1) {
        return;
    }
    switch (jqXHR.status) {
        case 403:
            console.log('Forbidden, go away!');
            break;
        case 404:
            console.log('Resource not found :O');
            break;
        case 422:
            console.log('Unprocessable entity.');
            break;
        case 500:
            console.log('Someone have to pay for this!');
            break;
        default:
            console.log('Anyone knows what is going on here?');
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks!! Your solution actually works really well! i didn't noticed the handleErrors in jQuery docs. Great answer!
handleErrors is custom property, you can set the name whatever you want.
You're right, that's because the ajax settings are extended with $.extend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.