从 Chrome 获取 API 响应 2 分钟后抛出错误

use*_*452 5 javascript reactjs fetch-api

我在 React js 中有一个提取请求,它调用 API(Node js 后端服务器)。对于通过 Google Chrome 发出的任何耗时超过 2 分钟的请求,获取请求都会引发错误:

“获取失败”。

如果我传递的参数较少(即请求哪个服务器花费的时间少于 2 分钟),则它可以正常工作。

下面是我的代码。

 fetch(url , requestOptions)
          .then(response => response.blob())
          .then(blob => {
            const newBlob = new Blob( [blob], {type: 'application/zip'});
            const downloadUrl = URL.createObjectURL(newBlob)
                    let a = document.createElement("a"); 
                    a.href = downloadUrl;
                    a.download = "reports";
                    document.body.appendChild(a);
                    a.click();
                    alert(this.state.selectedReport + "Reports Downloaded Successfully")
      
          })
          .catch((error) => {
            alert("Error in downloading report " + error)
        })
Run Code Online (Sandbox Code Playgroud)

通过网上浏览,发现fetch请求中没有超时参数。

我尝试使用等待和异步,但它没有成功,并且还从下面的链接中得到答案。

删除 Fetch API 默认超时

我想获取后端服务器花费超过2分钟的数据。谁可以帮我这个事?

小智 1

  1. 尝试使用curl 复制请求。
  2. 如果您能够请求花费超过两分钟的报告,那么这是服务器超时,我们需要增加服务器端的超时。
  3. 如果您能够通过curl下载,那么您可以按照这篇文章https://dmitripavlutin.com/timeout-fetch-request/