簡體   English   中英

需要覆蓋 Axios post 請求的默認超時時間

[英]Need to override the default time out of Axios post request

當我使用 axios 從客戶端(React JS)向服務器(spring)發出 post 請求時,服務器的響應時間超過 2 分鍾。 因此,當需要更多 tan 2 分鍾時,客戶端不會等待接收響應。 所以我試圖用下面的代碼片段覆蓋默認超時。 但它不起作用。請幫助我解決問題。

const httpClient = Axios.create();
httpClient.defaults.timeout = 240000;

return httpClient.post(url, data).then(
 res => res
).catch(err => err);

如果您查看文檔(這是另一個主題,但顯示了超時示例)。

有兩種設置timeout

// Create an instance using the config defaults provided by the library
// At this point the timeout config value is `0` as is the default for the library
const instance = axios.create();

// Override timeout default for the library
// Now all requests using this instance will wait 2.5 seconds before timing out
instance.defaults.timeout = 2500;

// Override timeout for this request as it's known to take a long time
instance.get('/longRequest', {
  timeout: 5000
});

您可以使用instance.defaults.timeout覆蓋默認值或將其作為選項傳遞給您的調用。

您還可以在文檔中看到另一個示例

如果它不起作用,則可能是您使用的 axios 版本已過時,或者您遺漏了某些東西。

axios 實例的創建時間,該實例將應用於所有 api 調用

const httpClient = Axios.create({ timeout: 2 * 60 * 1000 });

您可以在 api 調用中傳遞timeout參數

httpClient.post(url, data, { timeout: 2 * 60 * 1000 })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM