Mar '20

Catching HTTP Codes In Javascript

I try to return the correct HTTP codes from my AJAX calls whenever possible, and I often use these for different purposes, for example a 401 indicating that a session has expired, and a 403 meaning that the permissions are lacking on the current session.
These can be caught and acted on very easily with jQuery:

$(document).ajaxError(function(event, jqxhr, settings, exception) {
    if(jqxhr.status == 401) {
    else if(jqxhr.status == 403) {
    else if(jqxhr.status >= 500) {
        alert('An error occurred');

Leave a Reply