|
12 | 12 | ### 带坑的解决方案一
|
13 | 13 | 我的经验有限,觉得唯一能做的,就是 axios 请求超时之后做一个重新请求。通过研究 axios 的使用说明,给它设置一个 timeout = 6000
|
14 | 14 |
|
15 |
| - |
16 | 15 | ```javascript
|
17 |
| -axios.defaults.timeout = 6000; |
| 16 | +axios.defaults.timeout = 6000 |
18 | 17 | ```
|
19 | 18 | 然后加一个栏截器.
|
20 | 19 |
|
21 | 20 | ```
|
22 | 21 | // Add a request interceptor
|
23 | 22 | axios.interceptors.request.use(function (config) {
|
24 | 23 | // Do something before request is sent
|
25 |
| - return config; |
| 24 | + return config |
26 | 25 | }, function (error) {
|
27 | 26 | // Do something with request error
|
28 |
| - return Promise.reject(error); |
29 |
| -}); |
| 27 | + return Promise.reject(error) |
| 28 | +}) |
30 | 29 |
|
31 | 30 | // Add a response interceptor
|
32 | 31 | axios.interceptors.response.use(function (response) {
|
33 | 32 | // Do something with response data
|
34 |
| - return response; |
| 33 | + return response |
35 | 34 | }, function (error) {
|
36 | 35 | // Do something with response error
|
37 |
| - return Promise.reject(error); |
38 |
| -}); |
| 36 | + return Promise.reject(error) |
| 37 | +}) |
39 | 38 | ```
|
| 39 | + |
40 | 40 | 这个栏截器作用是 如果在请求超时之后,栏截器可以捕抓到信息,然后再进行下一步操作,也就是我想要用 重新请求。
|
41 | 41 |
|
42 | 42 | 这里是相关的页面数据请求。
|
43 | 43 |
|
44 |
| - |
45 |
| - |
46 | 44 | ```
|
47 | 45 | this.$axios.get(url, {params:{load:'noload'}}).then(function (response) {
|
48 |
| - //dosomething(); |
| 46 | + //dosomething() |
49 | 47 | }).catch(error => {
|
50 | 48 | // 超时之后在这里捕抓错误信息.
|
51 | 49 | if (error.response) {
|
52 | 50 | console.log('error.response')
|
53 |
| - console.log(error.response); |
| 51 | + console.log(error.response) |
54 | 52 | } else if (error.request) {
|
55 | 53 | console.log(error.request)
|
56 | 54 | console.log('error.request')
|
57 | 55 | if(error.request.readyState == 4 && error.request.status == 0){
|
58 | 56 | // 我在这里重新请求
|
59 | 57 | }
|
60 | 58 | } else {
|
61 |
| - console.log('Error', error.message); |
| 59 | + console.log('Error', error.message) |
62 | 60 | }
|
63 |
| - console.log(error.config); |
64 |
| -}); |
| 61 | + console.log(error.config) |
| 62 | +}) |
65 | 63 |
|
66 | 64 | ```
|
67 |
| -超时之后, 报出 Uncaught (in promise) Error: timeout of xxx ms exceeded 的错误。 |
| 65 | +超时之后, 报出 `Uncaught (in promise) Error: timeout of xxx ms exceeded` 的错误。 |
68 | 66 |
|
69 | 67 | 在 catch 那里,它返回的是 error.request 错误,所以就在这里做 retry 的功能, 经过测试是可以实现重新请求的功功能, 虽然能够实现 超时重新请求的功能,但很麻烦,需要每一个请 API 的页面里边要设置重新请求。
|
70 | 68 |
|
71 | 69 | 如果项目有几十个.vue 文件,如果每个页面都要去设置超时重新请求的功能,那我要疯掉的.
|
72 | 70 |
|
73 | 71 | 而且这个机制还有一个严重的 bug,就是被请求的链接失效或其他原因造成无法正常访问的时候,这个机制失效了,它不会等待我设定的 6 秒,而且一直在刷,一秒种请求几十次,很容易就把服务器搞垮了,请看下图, 一眨眼的功能,它就请求了 146 次。
|
74 | 72 |
|
75 |
| - |
76 |
| - |
77 | 73 | ---
|
78 | 74 |
|
79 | 75 | ### 带坑的解决方案二
|
80 |
| -研究了 axios 的源代码,超时后, 会在拦截器那里 axios.interceptors.response 捕抓到错误信息, 且 error.code = "ECONNABORTED",具体链接 |
| 76 | +研究了 `axios` 的源代码,超时后, 会在拦截器那里 `axios.interceptors.response` 捕抓到错误信息, 且 `error.code = "ECONNABORTED"`,具体链接 |
81 | 77 |
|
82 | 78 | https://github.com/axios/axios/blob/26b06391f831ef98606ec0ed406d2be1742e9850/lib/adapters/xhr.js#L95-L101
|
83 | 79 |
|
84 | 80 | ```
|
85 | 81 | // Handle timeout
|
86 | 82 | request.ontimeout = function handleTimeout() {
|
87 | 83 | reject(createError('timeout of' + config.timeout + 'ms exceeded', config, 'ECONNABORTED',
|
88 |
| - request)); |
| 84 | + request)) |
89 | 85 |
|
90 | 86 | // Clean up request
|
91 |
| - request = null; |
92 |
| - }; |
| 87 | + request = null |
| 88 | + } |
93 | 89 | ```
|
94 |
| - |
95 | 90 |
|
96 | 91 | 所以,我的全局超时重新获取的解决方案这样的。
|
97 | 92 |
|
98 |
| - |
99 | 93 | ```javascript
|
100 | 94 | axios.interceptors.response.use(function(response) {
|
101 | 95 | // ...
|
102 | 96 | }, function(error){
|
103 |
| - const originalRequest = error.config; |
| 97 | + const originalRequest = error.config |
104 | 98 | if(error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1 && !originalRequest._retry){
|
105 | 99 | originalRequest._retry = true
|
106 |
| - return axios.request(originalRequest); |
| 100 | + return axios.request(originalRequest) |
107 | 101 | }
|
108 |
| -}); |
| 102 | +}) |
109 | 103 | ```
|
110 |
| - |
111 | 104 |
|
112 |
| -这个方法,也可以实现得新请求,但有两个问题,1 是它只重新请求 1 次,如果再超时的话,它就停止了,不会再请求。第 2 个问题是,我在每个有数据请求的页面那里,做了许多操作,比如 this.$axios.get(url).then 之后操作。 |
| 105 | +这个方法,也可以实现得新请求,但有两个问题,1 是它只重新请求 1 次,如果再超时的话,它就停止了,不会再请求。第 2 个问题是,我在每个有数据请求的页面那里,做了许多操作,比如 `this.$axios.get(url).then` 之后操作。 |
113 | 106 |
|
114 | 107 | ---
|
115 | 108 | ### 完美的解决方法
|
116 |
| -以 AOP 编程方式,我需要的是一个 超时重新请求的全局功能, 要在 axios.Interceptors 下功夫,在 github 的 axios 的 issue 找了别人的一些解决方法,终于找到了一个完美解决方案,就是下面这个。 |
| 109 | +以 `AOP` 编程方式,我需要的是一个 超时重新请求的全局功能, 要在 `axios.Interceptors` 下功夫,在 `Github` 的 `axios` 的 `issue` 找了别人的一些解决方法,终于找到了一个完美解决方案,就是下面这个。 |
117 | 110 |
|
118 | 111 | https://github.com/axios/axios/issues/164#issuecomment-327837467
|
119 | 112 |
|
120 |
| - |
121 |
| - |
122 | 113 | ```
|
123 | 114 | // 在 main.js 设置全局的请求次数,请求的间隙
|
124 |
| -axios.defaults.retry = 4; |
125 |
| -axios.defaults.retryDelay = 1000; |
| 115 | +axios.defaults.retry = 4 |
| 116 | +axios.defaults.retryDelay = 1000 |
126 | 117 |
|
127 | 118 | axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
|
128 |
| - var config = err.config; |
| 119 | + var config = err.config |
129 | 120 | // 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) |
131 | 122 |
|
132 | 123 | // Set the variable for keeping track of the retry count
|
133 |
| - config.__retryCount = config.__retryCount || 0; |
| 124 | + config.__retryCount = config.__retryCount || 0 |
134 | 125 |
|
135 | 126 | // Check if we've maxed out the total number of retries
|
136 | 127 | if(config.__retryCount>= config.retry) {
|
137 | 128 | // Reject with the error
|
138 |
| - return Promise.reject(err); |
| 129 | + return Promise.reject(err) |
139 | 130 | }
|
140 | 131 |
|
141 | 132 | // Increase the retry count
|
142 |
| - config.__retryCount += 1; |
| 133 | + config.__retryCount += 1 |
143 | 134 |
|
144 | 135 | // Create new promise to handle exponential backoff
|
145 | 136 | var backoff = new Promise(function(resolve) {
|
146 | 137 | setTimeout(function() {
|
147 |
| - resolve(); |
148 |
| - }, config.retryDelay || 1); |
149 |
| - }); |
| 138 | + resolve() |
| 139 | + }, config.retryDelay || 1) |
| 140 | + }) |
150 | 141 |
|
151 | 142 | // Return the promise in which recalls axios to retry the request
|
152 | 143 | return backoff.then(function() {
|
153 |
| - return axios(config); |
154 |
| - }); |
155 |
| -}); |
| 144 | + return axios(config) |
| 145 | + }) |
| 146 | +}) |
156 | 147 | ```
|
157 |
| -其他的那个几十个.vue 页面的 this.$axios 的 get 和 post 的方法根本就不需要去修改它们的代码。 |
| 148 | +其他的那个几十个 `.vue` 页面的 `this.$axios` 的 `get` 和 `post` 的方法根本就不需要去修改它们的代码。 |
158 | 149 |
|
159 | 150 |
|
160 | 151 |
|
|
0 commit comments