Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing .ajaxError for being called if handled elsewhere

I have a global $(document).ajaxError handler that takes care of most errors for me just in case there is ever an unexpected 500 error or something. However, there are times when I want to catch errors locally in a script and then prevent that global error handler from being called. Is there a way to do this such as you would with an event.stopPropagation()?

Example

$.get('/something/', function() {
    alert("Stuff went well.");
}).error(function(response)) {
    if (response.status == 404) {
        alert('Page not found');
        // Do something to stop global ajaxError from being called.
    }
});
like image 431
intargc Avatar asked Dec 22 '22 07:12

intargc


1 Answers

You need to pass the global: false param to $.ajax like this:

$.ajax({
    url: '/something/',
    global: false,
    success: function() {
       alert('Success');
    }
}).error(function(response)) {
    if (response.status == 404) {
        alert('Page not found');
    }
});

Reference: Ajax Events, jQuery.get

like image 167
N Rohler Avatar answered Dec 24 '22 02:12

N Rohler