Javascript 如何在 jquery ajax 中处理 net::ERR_CONNECTION_REFUSED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29412865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle net::ERR_CONNECTION_REFUSED in jquery ajax
提问by sreginogemoh
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);
});
But the thing is that on https request my ajax is not working because service I am calling is not accessible via https and I am getting error :ERR_CONNECTION_REFUSED- so that is fine, I just want to handle that. I have .failin ajax call, but it is not handling :ERR_CONNECTION_REFUSEDCould you give advice on how to handle :ERR_CONNECTION_REFUSEDin that case?
但问题是,在 https 请求中,我的 ajax 无法正常工作,因为无法通过 https 访问我正在调用的服务,并且出现错误:ERR_CONNECTION_REFUSED- 所以没关系,我只想处理它。我有.failajax 调用,但它没有处理:ERR_CONNECTION_REFUSED你能就:ERR_CONNECTION_REFUSED在这种情况下如何处理提出建议吗?
I also was trying to wrap my ajax call to try-catchblock, but it wasn't working either.
我也试图将我的 ajax 调用包装起来以try-catch阻止,但它也不起作用。
回答by mohamedrias
You can use timeoutproperty of $.ajaxto fire the errorcallback.
您可以使用timeoutof 属性$.ajax来触发error回调。
This will make sure that the errorcallback 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 failcallback when there is no internet as well.
fail当没有互联网时,这也会触发回调。
回答by Darwin Airola
It appears that when jqXHR.readyState (i.e., the readyState field of the first parameter to the $.ajax(...).fail() method) is 0 that a network error has occurred. However, I have not been able to ascertain what the exact network error is via JavaScript.
看起来当 jqXHR.readyState(即 $.ajax(...).fail() 方法的第一个参数的 readyState 字段)为 0 时,表示发生了网络错误。但是,我无法通过 JavaScript 确定确切的网络错误是什么。
I have looked at the jQuery Ajax code, and xhr.send() (i.e., the XMLHttpRequest.send()) method (which generates the network error) does not catch nor throw the error. Thus, it is not possible to catch it.
我查看了 jQuery Ajax 代码,并且 xhr.send()(即 XMLHttpRequest.send())方法(生成网络错误)不会捕获也不会抛出错误。因此,不可能抓住它。
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 没有注意到发生的特定类型的网络错误。
回答by user9330552
<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>
回答by BradW
So, failis being called if we get a ERR_CONNECTION_REFUSED, but there is no way to actually see why failis being called. Is there a way to detect inside of the fail method why things failed? i.e. timeout vs connection refused? There is a difference between the two. One I could possible retry, the other makes no difference.
所以,fail如果我们得到 a 就会被调用ERR_CONNECTION_REFUSED,但是没有办法真正看到为什么fail被调用。有没有办法检测失败方法内部为什么事情失败?即超时与连接被拒绝?两者之间存在差异。一个我可以重试,另一个没有区别。
You do not need to set timeout to handle connection refused failures.
您不需要设置超时来处理连接拒绝失败。

