70

設定しましたaxios.defaults.timeout = 1000;

API を提供するサーバーを停止しました。

ただし、リクエストを送信してからタイムアウトするまでに 1 秒以上かかります。

これは私の要求がどのように見えるかです:

import axios from 'axios';
axios.defaults.timeout = 1000;

return axios.post(`${ROOT_URL}/login/${role}`, creds).then((response) => {
      console.log(response);

        if(response.status === 200) {
          // If login was successful, set the token in local storage
          localStorage.setItem(`${role}_log_toks`, JSON.stringify(response.data));

          // Dispatch the success action
          dispatch(receiveLogin(response.data));

          return response;
        }
      }).catch(err => {
        console.log(err);
        // If there was a problem, we want to
        // dispatch the error condition
        if(err.data && err.status === 404) {
          dispatch(loginError(err.data));
        } else {
          dispatch(loginError('Please check your network connection and try again.'));
        }

        return err;
      });

私も試しました:

return axios.post(`${ROOT_URL}/login/${role}`, creds, {timeout: 1000}).then...

Axios はフェッチを停止せず、5 ~ 10 分後に最終的にネットワーク エラーが表示されます。タイムアウトを処理する他の手法があることは理解していますが、axios のタイムアウト機能が機能しないのはなぜですか? axios がフェッチを停止しない理由は何でしょうか?

アクシオス バージョン 0.9.1

編集: コメントで述べたように、私も試しました:

import axios from 'axios';

const httpClient = axios.create();

httpClient.defaults.timeout = 500;

return httpClient.post(`${ROOT_URL}/login/${role}`, creds)
  .then(handleResponse)
4

6 に答える 6

21

axios http クライアントのインスタンスを作成する必要があります。

const httpClient = axios.create();
httpClient.defaults.timeout = 500;

その後、次のように httpClient を使用できます。

return httpClient.post(`${ROOT_URL}/login/${role}`, creds)
  .then(handleResponse)

補足として、次を使用する代わりに、同じ構成でベース URL を設定することもできます${ROOT_URL}

httpClient.defaults.baseURL = ROOT_URL
于 2016-04-18T20:00:10.367 に答える