@@ -26,19 +26,8 @@ export const ajax = async (options) => {
26
26
const config = await mergeLocalOptions ( )
27
27
const core = ( retry = 3 ) => {
28
28
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 -- ) {
42
31
setTimeout ( ( ) => {
43
32
core ( retry - 1 )
44
33
} , 3e3 )
@@ -47,6 +36,22 @@ export const ajax = async (options) => {
47
36
noop ( options . error ) ( err , xhr )
48
37
}
49
38
}
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
50
55
if ( / p o s t / i. test ( options . method ) ) {
51
56
xhr . open ( options . method , options . url , options . async !== false )
52
57
xhr . setRequestHeader ( 'Content-type' , / j s o n / i. test ( options . dataType ) ? 'application/json' : 'application/x-www-form-urlencoded' )
0 commit comments