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?
Not in the statusCode parameter. But that’s because a better (and leaner) way if you want to catch everything: