axios 超时设置

问题

最近写了个大数据后台日志流统计数据,在本地测试的时候老是出现timeout,发现是机器太破,计算时间太长超出了axios的默认超时时间。

1. 全局设置网络超时

axios.defaults.timeout = 30000;

2. 单独对某个请求设置网络超时

axios.post(url, postInfo,{timeout:300000})
    .then((resp: any) => {
        console.log(resp.data);
    })
    .catch((error: any) => {
         console.error(error);
     })

3. 看axios原码,axios.post第三个参数AxiosRequestConfig可以配置其他

export interface AxiosRequestConfig {
  url?: string;
  method?: Method;
  baseURL?: string;
  transformRequest?: AxiosTransformer | AxiosTransformer[];
  transformResponse?: AxiosTransformer | AxiosTransformer[];
  headers?: any;
  params?: any;
  paramsSerializer?: (params: any) => string;
  data?: any;
  timeout?: number;
  withCredentials?: boolean;
  adapter?: AxiosAdapter;
  auth?: AxiosBasicCredentials;
  responseType?: ResponseType;
  xsrfCookieName?: string;
  xsrfHeaderName?: string;
  onUploadProgress?: (progressEvent: any) => void;
  onDownloadProgress?: (progressEvent: any) => void;
  maxContentLength?: number;
  validateStatus?: (status: number) => boolean;
  maxRedirects?: number;
  socketPath?: string | null;
  httpAgent?: any;
  httpsAgent?: any;
  proxy?: AxiosProxyConfig | false;
  cancelToken?: CancelToken;
}

本文参考

0%