jQuery - AjaxError

The jQuery ajaxError() method specifies a function to be run when an AJAX request fails.

The example below is designed NOT to work (bad file name) but and the error will be shown.

 


$(document).ready(function ()
{
    // only the ajax call can trigger this
    $(document).ajaxError(function () { ShowError(); });

    $("#btnError").click(function ()
    {
        // bad call
        $("#ajax-content").load("bad-file.txt");

        // good call - this will not trigger the ajaxError
        //$("#ajax-content").load("ajax-sample.txt");
    });

    const ShowError = () =>
    {
        $("#ajax-error").html("An error occurred...must be a bad file name.");
    };

});