Skip to content

Commit cbe2ba3

Browse files
committed
修复请求重试
1 parent 9bef1dc commit cbe2ba3

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/download.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,8 @@ export const ajax = async (options) => {
2626
const config = await mergeLocalOptions()
2727
const core = (retry = 3) => {
2828
const xhr = new XMLHttpRequest()
29-
options.method = options.method || 'get'
30-
xhr.responseType = options.dataType || 'json';
31-
xhr.onreadystatechange = () => {
32-
if (xhr.readyState == 4) {
33-
try {
34-
noop(options.success)(xhr.response, xhr)
35-
} catch (err) {
36-
noop(options.error)(err, xhr)
37-
}
38-
}
39-
}
40-
xhr.error = (err) => {
41-
if (retry) {
29+
const errorCB = (err) => {
30+
if (retry--) {
4231
setTimeout(() => {
4332
core(retry - 1)
4433
}, 3e3)
@@ -47,6 +36,22 @@ export const ajax = async (options) => {
4736
noop(options.error)(err, xhr)
4837
}
4938
}
39+
options.method = options.method || 'get'
40+
xhr.responseType = options.dataType || 'json'
41+
xhr.onreadystatechange = () => {
42+
if (xhr.readyState == 4) {
43+
if (xhr.status === 200) {
44+
try {
45+
noop(options.success)(xhr.response, xhr)
46+
} catch (err) {
47+
errorCB(err)
48+
}
49+
} else {
50+
errorCB(new Error('network error'))
51+
}
52+
}
53+
}
54+
xhr.error = errorCB
5055
if (/post/i.test(options.method)) {
5156
xhr.open(options.method, options.url, options.async !== false)
5257
xhr.setRequestHeader('Content-type', /json/i.test(options.dataType) ? 'application/json' : 'application/x-www-form-urlencoded')

0 commit comments

Comments
 (0)