如何抓网:: ERR_CONNECTION_REFUSED [英] How to catch net::ERR_CONNECTION_REFUSED

查看:419
本文介绍了如何抓网:: ERR_CONNECTION_REFUSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了吗,有没有办法赶上failed to load resource: net::ERR_CONNECTION_REFUSED:

Is there a way to catch failed to load resource: net::ERR_CONNECTION_REFUSED, I've tried:

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);
}

失败函数可以捕获,但是我没有得到有关该错误的信息,或者是:

The fail function could catch, but I got no information about the error, either it is:

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

或其他任何问题..问题将是,如何得到这种错误?

or anything else.. the question would be, how to get the kind of error?

推荐答案

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

I even tried to achieve the goal using 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在后台线程中运行,因此它的运行时错误是无法捕获.

Above snippet only gives generic error handling, while I am not getting exact reason behind the error. The try...catch statement fails to catch anything, because none of the functions inside try block is throwing any exceptions. It seems XMLHttpRequest is running in background thread, so its runtime error in not being catchable.

由于jQuery是实际上是JavaScript的库,因此$.post()的行为也相同,因为$.post()也在幕后使用XMLHttpRequest.

As jQuery is a library which is actually a javascript, it will also behave same for $.post() because $.post() is also using XMLHttpRequest behind the curtain.

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

Below is the jQuery version, which also will handle generic error, as we can not exactly know reason for error.

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请求网络错误背后的确切原因.我们只能捕获通用错误和其他一些已知的状态代码,例如

As javascript XMLHttpRequest() is still not efficient enough for handling different error states, we can not know exact reason behind the network error for AJAX requests. We can only capture generic errors and some other known status codes like

找不到文件的"404"

"404" for file not found

"500"表示服务器无响应

"500" for server not responding

可以从 https://www.w3.org/Protocols了解更多/rfc2616/rfc2616-sec10.html

这篇关于如何抓网:: ERR_CONNECTION_REFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆