在$.ajaxError方法中检索textStatus值

Retrieve textStatus value in $.ajaxError method

本文关键字:检索 textStatus 方法 ajaxError      更新时间:2023-09-26

我正在尝试删除$.ajax错误处理程序函数,并使用$.ajaxError。一切正常,但我在全局错误处理程序中找不到textStatus值
旧错误处理:

$.ajax({
    ...
    },
    error:
        function (data, textStatus, xmlHttpRequest)
        {
            alert(textStatus);
        }
});

新的错误处理:

$(document).ajaxError(
    function errorHandler(event, xhr, ajaxOptions, thrownError)
    {
        alert(fetch textStatus?);
    }

如何在ajaxError方法中获取textStatus值?

我认为xhr对象具有statusText属性,您可以尝试一下。

$(document).ajaxError(
    function errorHandler(event, xhr, ajaxOptions, thrownError)
    {
        alert(xhr.statusText);
    }
);

注:只有当服务器生成错误并返回http状态代码时,才会填充xhr的statusstatusText。但是,对于jQuery生成的错误(timeout、parserror、notmodified),这些字段将为空。我认为您应该在代码中处理这种情况。