當回應為空時重試 axios 請求
P粉863295057
P粉863295057 2024-03-19 17:20:54
0
1
261

await axios
    .post(
      executeBatch,
      {
        headers: {
          "Content-Type": "application/json",
          "x-access-token": localStorage.getItem("token"),
        },
      }
    )
    .then(
      (response) =>
        this.$store.commit(
          "insertOutputFile",
          response.data.outputFile._id
        ),
    );

  alert("You can download the result");

所以有時我會得到狀態碼為 200 的空響應, 正在考慮如果發生這種情況重試該請求, 我想知道解決這個問題的正確方法是什麼。

P粉863295057
P粉863295057

全部回覆(1)
P粉556159786

我認為 axios 攔截器適合你。

axios.interceptors.response.use((response) => {
    return response
  },
  async function(error) {
    const originalRequest = error.config;

    if () { // condition
      originalRequest._retry = true;
    }
  }
)

您可以在基本 redux 目錄中建立 setupAxios 文件,並從基本 redux 目錄中的 index.js 匯出它。

export {default as setupAxios} 來自 "./setupAxios";

並從根index.js檔案定義setupAxios

import * as _redux from "./redux";

_redux.setupAxios(axios, store);

順便說一句,我使用的是react.js,vue.js 中的它可能有點不同。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!