当前位置:   article > 正文

axios 设置超时时间_axios设置超时时间

axios设置超时时间

1. 全局设置网络超时

axios.defaults.timeout = 30000;在这里插入代码片 timeout时间单位是毫秒
  • 1

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

//设置时间超时,timeout时间单位是毫秒
axios.post(url, postInfo,{timeout:300000})
    .then((resp: any) => {
        console.log(resp.data);
    })
    .catch((error: any) => {
         console.error(error);
     })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/610905
推荐阅读
  

闽ICP备14008679号