如何防止长时间运行的 HTTP 请求超时?

问题描述 投票:0回答:1

我有一个 API,可以执行可能需要长时间运行的任务,最多可能需要 10 分钟。当我从 Postman 调用 API 时,效果很好,但是当我从浏览器(Chrome、Safari、Firefox)使用 API 时,请求在收到响应之前超时。

API是用NodeJS/Express编写的,我已经增加了服务器的超时时间。我还增加了用于进行 API 调用(请求-承诺)的 JavaScript 库的超时。即使请求在浏览器中超时,API 也会继续运行并完成。如何阻止浏览器请求超时?

javascript ajax rest google-chrome http
1个回答
0
投票

这些可能会有所帮助:

XMLHttpRequest 1

const xhr = new XMLHttpRequest();
// Set to 0 (which is default) to disable timeout
xhr.timeout = 5000; // Milliseconds

jQuery Ajax 2

$.ajax({
    url: '/some/path',
    timeout:5000 // Milliseconds
})

Node.js HTTP 模块 3

const options = { timeout: 5000 /* Milliseconds */ };
const request = http.request(options, response => {
    // ...
});

获取API 4

似乎不支持任何指定超时的方式。

© www.soinside.com 2019 - 2024. All rights reserved.