Skip to content

Commit ae2785d

Browse files
committed
update: README.md
1 parent 3b0901a commit ae2785d

File tree

1 file changed

+37
-46
lines changed

1 file changed

+37
-46
lines changed

laravel/readme/21.2019-05-13-解决在vue中axios请求超时的问题.md

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,149 +12,140 @@
1212
### 带坑的解决方案一
1313
我的经验有限,觉得唯一能做的,就是 axios 请求超时之后做一个重新请求。通过研究 axios 的使用说明,给它设置一个 timeout = 6000
1414

15-
1615
```javascript
17-
axios.defaults.timeout = 6000;
16+
axios.defaults.timeout = 6000
1817
```
1918
然后加一个栏截器.
2019

2120
```
2221
// Add a request interceptor
2322
axios.interceptors.request.use(function (config) {
2423
// Do something before request is sent
25-
return config;
24+
return config
2625
}, function (error) {
2726
// Do something with request error
28-
return Promise.reject(error);
29-
});
27+
return Promise.reject(error)
28+
})
3029
3130
// Add a response interceptor
3231
axios.interceptors.response.use(function (response) {
3332
// Do something with response data
34-
return response;
33+
return response
3534
}, function (error) {
3635
// Do something with response error
37-
return Promise.reject(error);
38-
});
36+
return Promise.reject(error)
37+
})
3938
```
39+
4040
这个栏截器作用是 如果在请求超时之后,栏截器可以捕抓到信息,然后再进行下一步操作,也就是我想要用 重新请求。
4141

4242
这里是相关的页面数据请求。
4343

44-
45-
4644
```
4745
this.$axios.get(url, {params:{load:'noload'}}).then(function (response) {
48-
//dosomething();
46+
//dosomething()
4947
}).catch(error => {
5048
// 超时之后在这里捕抓错误信息.
5149
if (error.response) {
5250
console.log('error.response')
53-
console.log(error.response);
51+
console.log(error.response)
5452
} else if (error.request) {
5553
console.log(error.request)
5654
console.log('error.request')
5755
if(error.request.readyState == 4 && error.request.status == 0){
5856
// 我在这里重新请求
5957
}
6058
} else {
61-
console.log('Error', error.message);
59+
console.log('Error', error.message)
6260
}
63-
console.log(error.config);
64-
});
61+
console.log(error.config)
62+
})
6563
6664
```
67-
超时之后, 报出 Uncaught (in promise) Error: timeout of xxx ms exceeded 的错误。
65+
超时之后, 报出 `Uncaught (in promise) Error: timeout of xxx ms exceeded` 的错误。
6866

6967
在 catch 那里,它返回的是 error.request 错误,所以就在这里做 retry 的功能, 经过测试是可以实现重新请求的功功能, 虽然能够实现 超时重新请求的功能,但很麻烦,需要每一个请 API 的页面里边要设置重新请求。
7068

7169
如果项目有几十个.vue 文件,如果每个页面都要去设置超时重新请求的功能,那我要疯掉的.
7270

7371
而且这个机制还有一个严重的 bug,就是被请求的链接失效或其他原因造成无法正常访问的时候,这个机制失效了,它不会等待我设定的 6 秒,而且一直在刷,一秒种请求几十次,很容易就把服务器搞垮了,请看下图, 一眨眼的功能,它就请求了 146 次。
7472

75-
76-
7773
---
7874

7975
### 带坑的解决方案二
80-
研究了 axios 的源代码,超时后, 会在拦截器那里 axios.interceptors.response 捕抓到错误信息, 且 error.code = "ECONNABORTED",具体链接
76+
研究了 `axios` 的源代码,超时后, 会在拦截器那里 `axios.interceptors.response` 捕抓到错误信息, 且 `error.code = "ECONNABORTED"`,具体链接
8177

8278
https://github.com/axios/axios/blob/26b06391f831ef98606ec0ed406d2be1742e9850/lib/adapters/xhr.js#L95-L101
8379

8480
```
8581
// Handle timeout
8682
request.ontimeout = function handleTimeout() {
8783
reject(createError('timeout of' + config.timeout + 'ms exceeded', config, 'ECONNABORTED',
88-
request));
84+
request))
8985
9086
// Clean up request
91-
request = null;
92-
};
87+
request = null
88+
}
9389
```
94-
9590

9691
所以,我的全局超时重新获取的解决方案这样的。
9792

98-
9993
```javascript
10094
axios.interceptors.response.use(function(response) {
10195
// ...
10296
}, function(error){
103-
const originalRequest = error.config;
97+
const originalRequest = error.config
10498
if(error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1 && !originalRequest._retry){
10599
originalRequest._retry = true
106-
return axios.request(originalRequest);
100+
return axios.request(originalRequest)
107101
}
108-
});
102+
})
109103
```
110-
111104

112-
这个方法,也可以实现得新请求,但有两个问题,1 是它只重新请求 1 次,如果再超时的话,它就停止了,不会再请求。第 2 个问题是,我在每个有数据请求的页面那里,做了许多操作,比如 this.$axios.get(url).then 之后操作。
105+
这个方法,也可以实现得新请求,但有两个问题,1 是它只重新请求 1 次,如果再超时的话,它就停止了,不会再请求。第 2 个问题是,我在每个有数据请求的页面那里,做了许多操作,比如 `this.$axios.get(url).then` 之后操作。
113106

114107
---
115108
### 完美的解决方法
116-
以 AOP 编程方式,我需要的是一个 超时重新请求的全局功能, 要在 axios.Interceptors 下功夫,在 github 的 axios 的 issue 找了别人的一些解决方法,终于找到了一个完美解决方案,就是下面这个。
109+
`AOP` 编程方式,我需要的是一个 超时重新请求的全局功能, 要在 `axios.Interceptors` 下功夫,在 `Github``axios``issue` 找了别人的一些解决方法,终于找到了一个完美解决方案,就是下面这个。
117110

118111
https://github.com/axios/axios/issues/164#issuecomment-327837467
119112

120-
121-
122113
```
123114
// 在 main.js 设置全局的请求次数,请求的间隙
124-
axios.defaults.retry = 4;
125-
axios.defaults.retryDelay = 1000;
115+
axios.defaults.retry = 4
116+
axios.defaults.retryDelay = 1000
126117
127118
axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
128-
var config = err.config;
119+
var config = err.config
129120
// If config does not exist or the retry option is not set, reject
130-
if(!config || !config.retry) return Promise.reject(err);
121+
if(!config || !config.retry) return Promise.reject(err)
131122
132123
// Set the variable for keeping track of the retry count
133-
config.__retryCount = config.__retryCount || 0;
124+
config.__retryCount = config.__retryCount || 0
134125
135126
// Check if we've maxed out the total number of retries
136127
if(config.__retryCount>= config.retry) {
137128
// Reject with the error
138-
return Promise.reject(err);
129+
return Promise.reject(err)
139130
}
140131
141132
// Increase the retry count
142-
config.__retryCount += 1;
133+
config.__retryCount += 1
143134
144135
// Create new promise to handle exponential backoff
145136
var backoff = new Promise(function(resolve) {
146137
setTimeout(function() {
147-
resolve();
148-
}, config.retryDelay || 1);
149-
});
138+
resolve()
139+
}, config.retryDelay || 1)
140+
})
150141
151142
// Return the promise in which recalls axios to retry the request
152143
return backoff.then(function() {
153-
return axios(config);
154-
});
155-
});
144+
return axios(config)
145+
})
146+
})
156147
```
157-
其他的那个几十个.vue 页面的 this.$axios 的 get 和 post 的方法根本就不需要去修改它们的代码。
148+
其他的那个几十个 `.vue` 页面的 `this.$axios``get``post` 的方法根本就不需要去修改它们的代码。
158149

159150

160151

0 commit comments

Comments
 (0)