設定しました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 がフェッチを停止しない理由は何でしょうか?
編集: コメントで述べたように、私も試しました:
import axios from 'axios';
const httpClient = axios.create();
httpClient.defaults.timeout = 500;
return httpClient.post(`${ROOT_URL}/login/${role}`, creds)
.then(handleResponse)