1. Axios sets the network timeout globally

axios.defaults.timeout = 10 * 1000; // 10s

2. Set a network timeout for a specific request

axios.post(url, params, {timeout: 1000}) .then(res => { console.log(res); }) .catch(err=> { console.log(err); })})

3. Webpack dev proxyTable timeout

Dev: {// Paths assetsSubDirectory: 'static', '/', / / path / / proxy configuration table, here you can configure a particular request agent to the corresponding API interface / / method of use: https://vuejs-templates.github.io/webpack/proxy.html proxyTable: {'/ API ': {timeout: 30000, // Request timeout time target: 'http://127.0.0.1:3006', // Target interface domain name changeOrigin: PathRewrite: {'^/ API ': '// override interface}}, // Various Dev Server Settings host: 'localhost', // can be overwritten by process.env.HOST port: 4200, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined }Copy the code

4. The AXIos request times out

Sometimes the project needs to re-request the interface once or more if the request times out or fails to get data. The configuration procedure and method are as follows:

Since rerequest Settings are set when the request times out or fails to retrieve data, we must set them in the request-return interceptor

import axios from "axios"; Const Axios = axios.create({// The following attributes are used to set the retry times and the retry time: 2, // retryInterval: 1000 // Find the interval...... }); / / request before intercept Axios. Interceptors. Request. Use (config = > {return config}, function (error) {return Promise. Reject (error)}); / / request return data interception Axios. Interceptors. Response. Use (res = > {return res}, function axiosRetryInterceptor(res) { var config = res.config; // If the configuration does not exist or the retry property is not set, throw the promise error if (! config || ! config.retry) return Promise.reject(res); / / set a variable records to request the number of times the config. RetryCount = config. RetryCount | | 0; If (config.retrycount >= config.retry) {return promise.reject (res); } // The number of rerequests is increased config.retryCount += 1; Var back = new Promise(function(resolve) {console.log(" interface "+config.url); ") setTimeout(function() {resolve(); }, config.retryInterval|| 1); }); Then (function() {return axios (config); }); }); export default AxiosCopy the code

The above content is not original, excerpts from notes on the Internet