axios 库中的超时功能不起作用 [英] Timeout feature in the axios library is not working

查看:29
本文介绍了axios 库中的超时功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了 axios.defaults.timeout = 1000;

我停止了为我提供 API 的服务器.

I stopped the server that provides me with the APIs.

但是发送请求后超时时间超过1s.

But it takes more than 1s to timeout after sending a request.

这是我的请求的样子:

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 不停止获取的原因可能是什么?

Axios doesn't stop fetching and after 5 - 10 minutes it finally shows network error. I understand that there are other techniques to handle timeout but why doesn't the timeout feature in axios work? What could be the reason that axios doesn't stop fetching?

Axios 0.9.1 版

正如评论中提到的,我也尝试过:

As mentioned in the comments, I have also tried:

import axios from 'axios';

const httpClient = axios.create();

httpClient.defaults.timeout = 500;

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

推荐答案

来自这个 axios issue(感谢zhuyifan2013给出解决方案),我发现axios timeout响应超时不是连接超时.

From this axios issue (Thanks to zhuyifan2013 for giving the solution), I've found that axios timeout is response timeout not connection timeout.

假设您已经通过 axios 请求了 URL,并且服务器需要很长时间来响应,在这种情况下 axios 超时将起作用.

Let say you've requested the URL through axios and server is taking long time to respond, in this case the axios timeout will work.

但是您没有互联网连接,或者您请求的 IP 地址或域名不存在,在这种情况下 axios 超时将不起作用.

But you don't have internet connection or the IP address or domain name that you're requesting not there, in this case axios timeout will not work.

您必须使用以下代码

  const source = CancelToken.source();
  const timeout = setTimeout(() => {
    source.cancel();
    // Timeout Logic
  }, 10000);
  
  axios.get(ip + '/config', {cancelToken: source.token}).then((result) => {
    // Clear The Timeout
    clearTimeout(timeout);

    // Handle your response
  });

请注意,如果您有有效的连接,超时逻辑 块仍会被执行.所以你必须清除timeout.

Please note that if you've valid connection, still the Timeout Logic block will get executed. So you've to clear the timeout.

这篇关于axios 库中的超时功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆