Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Overriding jQuery default ajaxError for a specific status code

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!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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?');
    }
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!