jQuery Ajax Error Handling Function

$( document ).ajaxError(function( event, request, settings ) {
  $( "#msg" ).append( "
  • Error requesting page " + settings.url + "
  • " ); });

    Offical .ajaxError() API

    Note: If $.ajax() or $.ajaxSetup() is called with the global option set to false, the .ajaxError() method will not fire.

    Below is an older function which you can get the status from the jqXHR object.

    $(function() {
            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);
    Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

    Free Guide:

    How to Choose the Right Charting Library for Your Application

    How do you make sure that the charting library you choose has everything you need? Sign up to receive this detailed guide from FusionCharts, which explores all the factors you need to consider before making the decision.

    • lsoares13

      “error” is deprecated. besides, usage of ajaxSetup is not recommended by jQuery documentation.

    • Chris Tierney

      my Aunty Leah recently got a fantastic black Kia Forte Sedan just by some part time working online with a computer. try this BIG44.­c­o­m

    Special Offer
    Free course!

    Git into it! Bonus course Introduction to Git is yours when you take up a free 14 day SitePoint Premium trial.