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.
}
});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With