The required parameters in the Ajax error function are jqXHR, exception , and you can use it as shown below:
$.ajax({ url: 'some_unknown_page.html', success: function (response) { $('#post').html(response.responseText); }, error: function (jqXHR, exception) { var msg = ''; if (jqXHR.status === 0) { msg = 'Not connect.\n Verify Network.'; } else if (jqXHR.status == 404) { msg = 'Requested page not found. [404]'; } else if (jqXHR.status == 500) { msg = 'Internal Server Error [500].'; } else if (exception === 'parsererror') { msg = 'Requested JSON parse failed.'; } else if (exception === 'timeout') { msg = 'Time out error.'; } else if (exception === 'abort') { msg = 'Ajax request aborted.'; } else { msg = 'Uncaught Error.\n' + jqXHR.responseText; } $('#post').html(msg); }, });
DEMO FIDDLE
Options
jqXHR:
This is actually an error object that looks like

You can also view this in your own browser console using console.log inside the error function, for example:
error: function (jqXHR, exception) { console.log(jqXHR);
We use the status property of this object to get an error code, for example, if we get status = 404, this means that the requested page was not found. It does not exist at all. Based on this status code, we can redirect users to the login page or regardless of our business logic.
an exception:
This is a string variable that indicates the type of exception. So, if we get the 404 error, the exception text will just be an βerrorβ. In the same way, we can get a "timeout", "abort" like other exception texts.
Wear notification: jqXHR.success() , jqXHR.error() and jqXHR.complete() are deprecated from jQuery 1.8. To prepare your code for their possible removal, use jqXHR.done() , jqXHR.fail() , and jqXHR.always() .
So, if you are using jQuery 1.8 or higher , we will need to update the logic of the success and error functions, for example: -
// Assign handlers immediately after making the request, // and remember the jqXHR object for this request var jqxhr = $.ajax("some_unknown_page.html") .done(function (response) { // success logic here $('#post').html(response.responseText); }) .fail(function (jqXHR, exception) { // Our error logic here var msg = ''; if (jqXHR.status === 0) { msg = 'Not connect.\n Verify Network.'; } else if (jqXHR.status == 404) { msg = 'Requested page not found. [404]'; } else if (jqXHR.status == 500) { msg = 'Internal Server Error [500].'; } else if (exception === 'parsererror') { msg = 'Requested JSON parse failed.'; } else if (exception === 'timeout') { msg = 'Time out error.'; } else if (exception === 'abort') { msg = 'Ajax request aborted.'; } else { msg = 'Uncaught Error.\n' + jqXHR.responseText; } $('#post').html(msg); }) .always(function () { alert("complete"); });
Hope this helps!