【问题标题】:How to catch net::ERR_CONNECTION_REFUSED如何捕获 net::ERR_CONNECTION_REFUSED
【发布时间】:2015-04-17 20:20:38
【问题描述】:

有没有办法抓到failed to load resource: net::ERR_CONNECTION_REFUSED,我试过了:

try {
  $.post('',{},function(res) {
  }).fail(function (xhr, textStatus, errorThrown) { 
    xhr.textStatus = textStatus;
    xhr.errorThrown = errorThrown;
    console.log('fail',xhr);
    // how to get the 'ERR_CONNECTION_REFUSED' or anything else as string?
  });
} catch(e) {
  console.log('catch',e);
}

fail 函数可以捕获,但我没有得到有关错误的信息,要么是:

  • ERR_NAME_NOT_RESOLVED
  • ERR_CONNECTION_REFUSED
  • ERR_BLOCKED_BY_CLIENT
  • ERR_TUNNEL_CONNECTION_FAILED(使用代理时)

或其他任何东西.. 问题是,如何得到那种错误?

【问题讨论】:

  • 您不会有意获取有关错误的具体信息(因为否则可能会被滥用以深入了解用户的内部网络)。

标签: javascript jquery ajax exception


【解决方案1】:

我什至尝试使用 javascript XMLHttpRequest() 来实现目标

var xhttp= new XMLHttpRequest();
try{
  xhttp.onreadystatechange = function() {
    console.log(xhttp);
    if (xhttp.readyState == 4 && xhttp.status == 0) {
      alert("Unknown Error Occured. Server response not received.");
    }
  };
  xhttp.open("POST", "http://localhost:8080/data", true);
  xhttp.send();
}catch(e){
  console.log('catch', e);
}

上述 sn-p 仅提供通用错误处理,而我没有得到错误背后的确切原因。 try...catch 语句无法捕获任何内容,因为 try 块内的任何函数都没有抛出任何异常。 XMLHttpRequest 似乎在后台线程中运行,因此无法捕获其运行时错误。

由于 jQuery 是一个实际上是 javascript 的库,它对于 $.post() 的行为也相同,因为 $.post() 也在幕后使用 XMLHttpRequest

以下是 jQuery 版本,它也将处理一般错误,因为我们无法确切知道错误的原因。

try {
  $.post('http://localhost:8080/data', {}, function(res) {}).fail(function() {
      alert("Unknown Error Occured. Server response not received.");
  });
} catch (e) {
  console.log('catch', e);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

结论

由于 javascript XMLHttpRequest() 对于处理不同的错误状态仍然不够高效,我们无法知道 AJAX 请求网络错误背后的确切原因。我们只能捕获一般错误和其他一些已知状态代码,例如

找不到文件的“404”

“500”表示服务器没有响应

更多信息可以通过https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html了解

更新: 自从我上次更新这篇文章以来已经有很长时间了。我看到很少有答案试图实现类似的目标,但仍然收效甚微。正如这个线程中的一些答案所提到的,我们也可以使用XMLHttpRequest.onerror 回调函数来捕获一些通用错误,但如果您仍在使用 IE,那么它可能无法正常工作。

【讨论】:

  • 我很确定 state() 与网络无关,而是与 Deferred 对象有关。 “rejected”表示延迟对象已被拒绝而不是已解决。
  • 好的,我知道了。感谢您提及@Claudio。你能建议任何方法来检查网络拒绝吗?
  • 不 :( 我也在为这个问题苦苦挣扎,不幸的是没有我能想到的解决方案。也许我会调查 websocket 以查看网络问题的诊断是否可能是更有趣一点...
  • 你应该删除这个答案,因为它没有回答问题。
  • @Zero3 我目前正在尝试修改答案,使其能够满足问题要求。如果我失败了,那么我可能会删除答案。如果您知道任何答案,您可以在下面发布。我一看到答案就会删除。
【解决方案2】:
var xhttp= new XMLHttpRequest();
xhttp.onreadystatechange = function() {
console.log(xhttp);

xhttp.onerror = function(e){
    alert("Unknown Error Occured. Server response not received.");
};

xhttp.open("POST", "http://localhost:8080/data", true);
xhttp.send();

获取错误的另一种可能更容易理解的方法是 onerror 事件处理程序。据我所知,它不会为您提供比 Kirans 解决方案更有用的信息。

【讨论】:

  • 谢谢,至少它检测到发生了错误(与 .send 周围的 try-catch 似乎没有捕获不同)。我唯一关心的是浏览器对此的支持。如果不可用,除了使用超时之外,您还能如何检测网络级别的错误?可能值得检查 $.ajax 在承诺失败之前如何检测它。
【解决方案3】:

您可以在 chrome 中访问在线/离线。

var _Network_state = true;
    function updateIndicator() {
        // Show a different icon based on offline/online
        if (navigator.onLine) { // true|false
            // ... do other stuff
            _Network_state = true;
        } else {
            // ... do other stuff
            _Network_state = false;
        }
        console.info(_Network_state ? 'Online' : 'Offline');
    }
    // Update the online status icon based on connectivity
    window.addEventListener('online',  updateIndicator);
    window.addEventListener('offline', updateIndicator);
    updateIndicator();

在调用 ajax 之前,检查“_Network_state

【讨论】:

  • Chrome 以外的其他浏览器怎么样 - 仅适用于一种特定浏览器的东西不到 1/2 的答案。
  • 这真的不到答案的 2%,100 次中有 99 次 net::ERR_CONNECTION_REFUSED 错误只会在浏览器在线时发生,因为它表示从对等方收到 TCP 重置(通常意味着服务已关闭)。通常需要一定程度的网络访问权限。
  • 有关此功能的信息...得到很好的支持,但通常不足以判断用户是否可以真正连接到您的所有服务器caniuse.com/online-status
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
相关资源
最近更新 更多