繁体   English   中英

如何捕获.net::ERR_CONNECTION_REFUSED

[英]How to catch net::ERR_CONNECTION_REFUSED

有没有办法捕获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);
}

失败 function 可以捕捉到,但我没有得到有关错误的信息,要么是:

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

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

我什至尝试使用 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); }

上面的代码片段只给出了一般的错误处理,而我没有得到错误背后的确切原因。 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,那么它可能无法工作。

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 解决方案更有用的信息。

您可以在 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

当与服务器没有连接时(意味着服务器未运行),会发生这些类型的错误。 我遇到了这个错误,通过查看它没有任何消息,所以我的解决方案是捕获所有没有消息的错误,如下所示:

catch (err) {
  if(!err.data?.message) {
    console.log("Server error please try later!")
  }
}

我希望这能解决你的问题!

暂无
暂无

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

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