在jQuery Ajax中捕获“0”错误

Catch "0" errors in jQuery Ajax

提问人:frielmv 提问时间:9/9/2022 更新时间:9/9/2022 访问量:67

问:

使用此代码,我向服务器发送请求,并在错误部分检查请求是否成功,因此如果客户端未连接到互联网,我可以在 html 中显示“离线”消息:

$.get({
    url: '/files',
    success: update,
    error: requestError
});

function update(data)
{
    // do stuff with the data
}

function requestError(xhr)
{
    if(xhr.readyState == 0)
    {
        $('#files, #upload').css('display', 'none');
        $('#offline').css('display', 'inline-block');
    }
    else
        console.error(xhr.status);
}

这按预期工作:当请求成功时,它会运行该函数,当我关闭客户端的互联网时,它会在 html 中显示正确的错误消息。但是,即使我已经处理过了,它也会在控制台中抛出错误。这不会影响程序的功能,但会填满控制台。有什么方法可以防止出现此错误吗?根据这个答案,只捕获了 >=400 个错误,但它实际上并没有解释如何捕获其他错误。update()net::ERR_INTERNET_DISCONNECTED

JavaScript jQuery AJAX 错误处理

评论

0赞 freedomn-m 9/9/2022
关于这个问题,可能有一个相关的问题/.answer

答: 暂无答案