如何抓网::ERR_CONNECTION_REFUSED

问题描述 投票:0回答:5

有没有办法抓住

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
5个回答
25
投票

我什至尝试使用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,那么它可能不起作用。


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


1
投票

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

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

0
投票

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


0
投票

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);
}
xhttp.onreadystatechange

© www.soinside.com 2019 - 2024. All rights reserved.