捕获fetch err_connection_refused
我必须进行旧的谷歌搜索,因为如果您使用的是 javascript 的 fetch
而不是旧的 XMLHttpRequest
,我找不到任何关于如何执行此操作的线程,
我正在尝试测试是否或者我的虚拟机不在线。虚拟机的其余部分已被锁定,但我留下了一个开放端点用于测试。
我尝试使用 status
来检查服务器是否已启动,但给了我一个与响应错误不同的错误:
fetch("https://rockosmodernserver.westus2.cloudapp.azure.com/ ", {
method: "GET",
}).then(response => response)
.then(data => {
if (data.status == 200){
console.log("server is up")
}
else{
console.log("server is down!!")
}
})
如果服务器已启动,则它可以工作,但如果服务器已关闭,我会得到:
VM739:1 GET https://rockosmodernserver.westus2.cloudapp.azure.com/ net::ERR_CONNECTION_REFUSED
当我尝试谷歌搜索时我得到了 XMLHttpRequest
的解决方案,但没有得到 fetch 模块的解决方案。
I must be getting old google searches because I can't find any threads on how to do this if you are using javascript's fetch
not the old XMLHttpRequest
I am trying to test whether or not my VM is online. The rest of the VM is locked down but I left an open endpoint for testing.
I tried using the status
to check if the server is up but gave me a different error than a response error:
fetch("https://rockosmodernserver.westus2.cloudapp.azure.com/ ", {
method: "GET",
}).then(response => response)
.then(data => {
if (data.status == 200){
console.log("server is up")
}
else{
console.log("server is down!!")
}
})
It works if the server is up but if the server is down I get:
VM739:1 GET https://rockosmodernserver.westus2.cloudapp.azure.com/ net::ERR_CONNECTION_REFUSED
when I tried googling this I got solutions for XMLHttpRequest
but not for the fetch module.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找捕获块。在捕获块中,您可以获取错误。 IMN下面的示例您可以访问错误对象。
You looking for catch block. In the catch block you can fetch the errors. Imn the example below you have access to the error object.
如果服务器未响应,则
fetch
将其作为连接失败
并在catch()
block中处理。fetch
仅执行,然后()
在连接成功时阻止。如果连接未成功,则将执行catch()
块。If the server doesn't respond,
fetch
takes it asconnection failure
and handles it in thecatch()
block. Thefetch
only executesthen()
blocks when the connection is successful. If the connection was not successful, thecatch()
block will be executed.Checking that fetch was successful
如果服务器没有应答,他就无法给你回复。
这意味着没有状态代码。
更新:
当遇到网络错误或服务器端 CORS 配置错误时, fetch() Promise 将拒绝并返回 TypeError,尽管这通常意味着权限问题或类似问题 — 例如,404 并不构成网络错误。对成功 fetch() 的准确检查包括检查 Promise 是否已解析,然后检查 Response.ok 属性的值为 true。代码看起来像这样:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#checking_that_the_fetch_was_successful
第一次编辑:
通过使用超时并跟踪超时发生情况,此解决方案可能会有所帮助。
https://stackoverflow.com/a/50101022/13111978
If a server is not answering, he can not give you a response.
Which means there is no status code.
Update:
A fetch() promise will reject with a TypeError when a network error is encountered or CORS is misconfigured on the server-side, although this usually means permission issues or similar — a 404 does not constitute a network error, for example. An accurate check for a successful fetch() would include checking that the promise resolved, then checking that the Response.ok property has a value of true. The code would look something like this:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#checking_that_the_fetch_was_successful
First edit:
This solution could be helpful, by using a timeout and track that timeout has happened.
https://stackoverflow.com/a/50101022/13111978