jQuery default for ajax statusCode

default, http-status-codes, jquery

Solution

Not in the statusCode parameter. But that's because a better (and leaner) way if you want to catch everything:

complete: function(jqXHR, textStatus) {
    switch (jqXHR.status) {
        case 200:
            alert("200 received! yay!");
            break;
        case 404:
            alert("404 received! boo!");
            break;
        default:
            alert("I don't know what I just got but it ain't good!");
    }
}

Problem

As far as I understand, one possibility to handle error results with ajax is the following one: ``` $.ajax({ url: someUrl, type: 'POST', success: function(data) {}, error: function(jqXHR, exception) { if (jqXHR.status === 0) { alert('Not connect.\n Verify Network.'); } else if (jqXHR.status == 404) { alert('Requested page not found. [404]'); } else if (jqXHR.status == 500) { alert('Internal Server Error [500].'); } else if (exception === 'parsererror') { alert('Requested JSON parse failed.'); } else if (exception === 'timeout') { alert('Time out error.'); } else if (exception === 'abort') { alert('Ajax request aborted.'); } else { alert('Uncaught Error.\n' + jqXHR.responseText); } } }); ``` Or use `statusCode` in order to make it more readable: ``` $.ajax({ url: someUrl, type: 'POST', statusCode: { 200: function(data) { : : }, 401: function() { : : }, : : ``` My question is: Is it possible to use `statusCode` and have a default fall-through for it?

Original source