如何捕获 jQuery AJAX 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18864734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:38:38  来源:igfitidea点击:

How can I catch jQuery AJAX errors?

javascriptjqueryajaxerror-handling

提问by AR.

When an AJAX request is submitted to a site, server-side errors are easily handled with the jQuery promise approach. .done(), .fail(), etc. However for some requests (e.g. to an invalid site or one that doesn't accept cross-origin requests), an exception occurs immediately as the call is made. Here's an example of one error in the console:

当一个 AJAX 请求被提交到一个站点时,服务器端的错误很容易用 jQuery 承诺方法来处理。.done(), .fail(), 等等。但是对于某些请求(例如,对无效站点或不接受跨域请求的站点),在进行调用时会立即发生异常。这是控制台中一个错误的示例:

XMLHttpRequest cannot load http://someotherserver/api/blahblah. Origin http://localhost:52625is not allowed by Access-Control-Allow-Origin.

XMLHttpRequest 无法加载http://someotherserver/api/blahblah。原产地 http://localhost:52625不被访问控制允许来源允许的。

Yes, I know about CORS...that's not the issue. What I'm actually doing is trying a web api call to test if the server IP/name is correct

是的,我知道 CORS ……这不是问题。我实际上在做的是尝试调用 web api 来测试服务器 IP/名称是否正确

I'm aware of the erroroption in the jQuery request syntax:

我知道errorjQuery 请求语法中的选项:

$.ajax({
    url: remoteURL,
    type: 'GET',
    error: function (err) {
        console.log("AJAX error in request: " + JSON.stringify(err, null, 2));
    }
}).etc.etc.

The error is handled here, but exceptions are still logged in the console. It seemed reasonable to wrap the above in a try-catchblock, but that doesn't seem to help.

错误在此处处理,但异常仍记录在控制台中。将上述内容包装在一个try-catch块中似乎是合理的,但这似乎无济于事。

I've found this question, but the solution involves hacking the jQuery code. Surely there's a better way to catch these errors and not clog up the console logs??

我发现了这个问题,但解决方案涉及破解 jQuery 代码。当然有更好的方法来捕获这些错误而不是阻塞控制台日志?

回答by Mr.Manhattan

try this:

尝试这个:

$.ajax({
    url: remoteURL,
    type: 'GET',
    error: function (err) {
        console.log("AJAX error in request: " + JSON.stringify(err, null, 2));
    }
}).always(function(jqXHR, textStatus) {
    if (textStatus != "success") {
        alert("Error: " + jqXHR.statusText);
    }
});

XHR Listener:

XHR 监听器:

$.ajax({
    url: remoteURL,
    type: 'GET',
    xhr: function(){
        var xhr = new window.XMLHttpRequest();
        xhr.addEventListener("error", function(evt){
            alert("an error occured");
        }, false);
        xhr.addEventListener("abort", function(){
            alert("cancelled");
        }, false);

        return xhr;
    },
    error: function (err) {
        console.log("AJAX error in request: " + JSON.stringify(err, null, 2));
    }
});

回答by Shreejibawa

You can use web developer console in google chrome. Press F12. And use Networks tab for checking response, And for JavaSvript and jQuery and Ajax errors you can use Console tab. :)

您可以在谷歌浏览器中使用 Web 开发者控制台。按 F12。并使用 Networks 选项卡检查响应,对于 JavaSvript 和 jQuery 和 Ajax 错误,您可以使用 Console 选项卡。:)

Try this by adding to your ajax function :

通过添加到您的 ajax 函数来试试这个:

error: function(XMLHttpRequest, textStatus, errorThrown) {
 alert(errorThrown);