简体   繁体   English

如何在 jquery ajax 中处理 net::ERR_CONNECTION_REFUSED

[英]How to handle net::ERR_CONNECTION_REFUSED in jquery ajax

I have this kind of javascript:我有这种javascript:

$.ajax({
    url: "//myapi.com/json",
    dataType: "jsonp"
}).done(function (data) {
    selectText('Id', data.country);
}).fail(function (jqXHR, textStatus, errorThrown) {
    var defaultOption = 'US'
    selectDropdownByText('Id', defaultOption);

    console.log(errorThrown);
});

You can use timeout property of $.ajax to fire the error callback.您可以使用$.ajax timeout属性来触发error回调。

This will make sure that the error callback is fired if the server doesn't respond within a particular time limit.如果服务器在特定时间限制内没有响应,这将确保触发error回调。

$.ajax({
    url: "//myapi.com/json",
    dataType: "jsonp",
    timeout: 15000 // adjust the limit. currently its 15 seconds
}).done(function (data) {
    selectText('Id', data.country);
}).fail(function (jqXHR, textStatus, errorThrown) {
    var defaultOption = 'US'
    selectDropdownByText('Id', defaultOption);

    console.log(errorThrown);
});

This will trigger the fail callback when there is no internet as well.当没有互联网时,这也会触发fail回调。

It appears that when jqXHR.readyState (ie, the readyState field of the first parameter to the $.ajax(...).fail() method) is 0 that a network error has occurred.看起来当 jqXHR.readyState(即 $.ajax(...).fail() 方法的第一个参数的 readyState 字段)为 0 时,表示发生了网络错误。 However, I have not been able to ascertain what the exact network error is via JavaScript.但是,我无法通过 JavaScript 确定确切的网络错误是什么。

I have looked at the jQuery Ajax code, and xhr.send() (ie, the XMLHttpRequest.send()) method (which generates the network error) does not catch nor throw the error.我查看了 jQuery Ajax 代码,并且 xhr.send()(即 XMLHttpRequest.send())方法(生成网络错误)不会捕获也不会抛出错误。 Thus, it is not possible to catch it.因此,不可能抓住它。

It appears that the browser detects and displays the correct error message but that jQuery is oblivious to the specific type of network error that occurs.浏览器似乎检测并显示了正确的错误消息,但 jQuery 没有注意到发生的特定类型的网络错误。

So, fail is being called if we get a ERR_CONNECTION_REFUSED , but there is no way to actually see why fail is being called.所以, fail ,如果我们得到一个是被称为ERR_CONNECTION_REFUSED ,但也没有办法真正明白为什么fail被调用。 Is there a way to detect inside of the fail method why things failed?有没有办法检测失败方法内部为什么事情失败? ie timeout vs connection refused?即超时与连接被拒绝? There is a difference between the two.两者之间存在差异。 One I could possible retry, the other makes no difference.一个我可以重试,另一个没有区别。

You do not need to set timeout to handle connection refused failures.您不需要设置超时来处理连接拒绝失败。

    <code>$.ajax({
         url: "//myapi.com/json", //this is problem
         dataType: "jsonp"
    }).done(function (data) {
         selectText('Id', data.country);
    }).fail(function (jqXHR, textStatus, errorThrown) {
        var defaultOption = 'US'
        selectDropdownByText('Id', defaultOption);

        console.log(errorThrown);
    });</code>

relative path is not good,相对路径不好,

 <code>$.ajax({
        url: "myapi.com/json", //this will work
        dataType: "jsonp"
    }).done(function (data) {
        selectText('Id', data.country);
    }).fail(function (jqXHR, textStatus, errorThrown) {
        var defaultOption = 'US'
        selectDropdownByText('Id', defaultOption);

        console.log(errorThrown);
    });</code>

If you use this fail function it will get caught in the jqXHR.status===0 condition.如果您使用此失败函数,它将陷入 jqXHR.status===0 条件。

        .fail(function (jqXHR, exception) {
            // Our error logic here
            var msg = '';
            if (jqXHR.status === 0) {
                msg = 'No connection.\n Verify Network.';
                //ERR_CONNECTION_REFUSED hits this one
            } else if (jqXHR.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                msg = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + jqXHR.responseText;
            }
             $('#info').html("<p class='alert alert-danger msg'>Error: " + msg + "</p>");
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM