捕获fetch err_connection_refused

发布于 2025-01-18 01:11:35 字数 878 浏览 1 评论 0原文

我必须进行旧的谷歌搜索,因为如果您使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

通知家属抬走 2025-01-25 01:11:35

您正在寻找捕获块。在捕获块中,您可以获取错误。 IMN下面的示例您可以访问错误对象。

fetch("https://rockosmodernserver.westus2.cloudapp.azure.com/ ", {
    method: "GET",
    }).then(response => response)
    .then(data => {
        console.log("server is up")
    })
    .catch((error) => {
      console.error('Error:', error);
      console.log("server is down!!")   
    });

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("https://rockosmodernserver.westus2.cloudapp.azure.com/ ", {
    method: "GET",
    }).then(response => response)
    .then(data => {
        console.log("server is up")
    })
    .catch((error) => {
      console.error('Error:', error);
      console.log("server is down!!")   
    });

停顿的约定 2025-01-25 01:11:35

如果服务器未响应,则fetch将其作为连接失败并在catch() block中处理。 fetch仅执行,然后()在连接成功时阻止。如果连接未成功,则将执行catch()块。

fetch('something')
 .then( response => {})
 .catch(error => {
   // handle error here
 })

If the server doesn't respond, fetch takes it as connection failure and handles it in the catch() block. The fetch only executes then() blocks when the connection is successful. If the connection was not successful, the catch() block will be executed.

fetch('something')
 .then( response => {})
 .catch(error => {
   // handle error here
 })

Checking that fetch was successful

时光病人 2025-01-25 01:11:35

如果服务器没有应答,他就无法给你回复。
这意味着没有状态代码。

更新:
当遇到网络错误或服务器端 CORS 配置错误时, fetch() Promise 将拒绝并返回 TypeError,尽管这通常意味着权限问题或类似问题 — 例如,404 并不构成网络错误。对成功 fetch() 的准确检查包括检查 Promise 是否已解析,然后检查 Response.ok 属性的值为 true。代码看起来像这样:

  fetch('flowers.jpg')
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not OK');
    }
    return response.blob();
  })
  .then(myBlob => {
    myImage.src = URL.createObjectURL(myBlob);
  })
  .catch(error => {
    console.error('There has been a problem with your fetch operation:', error);
  });

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:

  fetch('flowers.jpg')
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not OK');
    }
    return response.blob();
  })
  .then(myBlob => {
    myImage.src = URL.createObjectURL(myBlob);
  })
  .catch(error => {
    console.error('There has been a problem with your fetch operation:', error);
  });

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文