Heim  >  Fragen und Antworten  >  Hauptteil

Wiederholen Sie die Axios-Anfrage, wenn die Antwort leer ist

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");

Manchmal bekomme ich eine leere Antwort mit dem Statuscode 200, Erwägen Sie in diesem Fall einen erneuten Versuch der Anfrage. Ich möchte wissen, wie man dieses Problem richtig löst.

P粉863295057P粉863295057213 Tage vor260

Antworte allen(1)Ich werde antworten

  • 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} from "./setupAxios";

    并从根index.js文件定义setupAxios

    import * as _redux from "./redux";
    
    _redux.setupAxios(axios, store);

    顺便说一句,我使用的是react.js,vue.js 中的它可能有点不同。

    Antwort
    0
  • StornierenAntwort