Jquery Ajax error handling to ignore aborted

后端 未结 9 733
栀梦
栀梦 2020-12-07 17:35

I want to have a global error handling method for ajax calls, this is what I have now:

$.ajaxSetup({
  error: function (XMLHttpRequest, textStatus, errorThro         


        
9条回答
  •  春和景丽
    2020-12-07 17:56

    Building upon Alastair Pitts'a answer, you can also do this to have more informative messages:

    $(document).ajaxError(function (e, jqXHR, ajaxSettings, thrownError)
    {
        {
            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);
            }
        }
    });
    

提交回复
热议问题