首頁  >  問答  >  主體

當回應為空時重試 axios 請求

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粉863295057P粉863295057213 天前259

全部回覆(1)我來回復

  • P粉556159786

    P粉5561597862024-03-20 00:57:20

    我認為 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 中的它可能有點不同。

    回覆
    0
  • 取消回覆