@@ -20,146 +20,147 @@ function ajax(options) {
2020 } else {
2121 return parseResponse ( xhr , options ) ;
2222 }
23- }
2423
25- ajax . defaults = {
26- async : true ,
27- dataType : "json" ,
28- timeout : 30 ,
29- type : "GET" ,
30- } ;
31- // contentType
32- // context
33- // data
34- // error
35- // headers
36- // progress
37- // success
38- // url
39-
40- var applyDefaults = function ( options ) {
41- for ( var key in ajax . defaults ) {
42- if ( options [ key ] == null ) {
43- options [ key ] = ajax . defaults [ key ] ;
24+ function applyDefaults ( options ) {
25+ for ( var key in ajax . defaults ) {
26+ if ( options [ key ] == null ) {
27+ options [ key ] = ajax . defaults [ key ] ;
28+ }
4429 }
4530 }
46- } ;
4731
48- var serializeData = function ( options ) {
49- if ( ! options . data ) {
50- return ;
51- }
32+ function serializeData ( options ) {
33+ if ( ! options . data ) {
34+ return ;
35+ }
5236
53- if ( options . type === "GET" ) {
54- options . url += "?" + serializeParams ( options . data ) ;
55- options . data = null ;
56- } else {
57- options . data = serializeParams ( options . data ) ;
37+ if ( options . type === "GET" ) {
38+ options . url += "?" + serializeParams ( options . data ) ;
39+ options . data = null ;
40+ } else {
41+ options . data = serializeParams ( options . data ) ;
42+ }
5843 }
59- } ;
6044
61- var serializeParams = ( params ) =>
62- Object . entries ( params )
63- . map (
64- ( [ key , value ] ) =>
65- `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( value ) } ` ,
66- )
67- . join ( "&" ) ;
68-
69- var applyCallbacks = function ( xhr , options ) {
70- if ( ! options . async ) {
71- return ;
45+ function serializeParams ( params ) {
46+ return Object . entries ( params )
47+ . map (
48+ ( [ key , value ] ) =>
49+ `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( value ) } ` ,
50+ )
51+ . join ( "&" ) ;
7252 }
7353
74- xhr . timer = setTimeout (
75- onTimeout . bind ( undefined , xhr , options ) ,
76- options . timeout * 1000 ,
77- ) ;
78- if ( options . progress ) {
79- xhr . onprogress = options . progress ;
80- }
81- xhr . onreadystatechange = function ( ) {
82- if ( xhr . readyState === 4 ) {
83- clearTimeout ( xhr . timer ) ;
84- onComplete ( xhr , options ) ;
54+ function applyCallbacks ( xhr , options ) {
55+ if ( ! options . async ) {
56+ return ;
8557 }
86- } ;
87- } ;
8858
89- var applyHeaders = function ( xhr , options ) {
90- if ( ! options . headers ) {
91- options . headers = { } ;
59+ xhr . timer = setTimeout (
60+ onTimeout . bind ( undefined , xhr , options ) ,
61+ options . timeout * 1000 ,
62+ ) ;
63+ if ( options . progress ) {
64+ xhr . onprogress = options . progress ;
65+ }
66+ xhr . onreadystatechange = function ( ) {
67+ if ( xhr . readyState === 4 ) {
68+ clearTimeout ( xhr . timer ) ;
69+ onComplete ( xhr , options ) ;
70+ }
71+ } ;
9272 }
9373
94- if ( options . contentType ) {
95- options . headers [ "Content-Type" ] = options . contentType ;
96- }
74+ function applyHeaders ( xhr , options ) {
75+ if ( ! options . headers ) {
76+ options . headers = { } ;
77+ }
9778
98- if (
99- ! options . headers [ "Content-Type" ] &&
100- options . data &&
101- options . type !== "GET"
102- ) {
103- options . headers [ "Content-Type" ] = "application/x-www-form-urlencoded" ;
104- }
79+ if ( options . contentType ) {
80+ options . headers [ "Content-Type" ] = options . contentType ;
81+ }
10582
106- if ( options . dataType ) {
107- options . headers [ "Accept" ] =
108- MIME_TYPES [ options . dataType ] || options . dataType ;
109- }
83+ if (
84+ ! options . headers [ "Content-Type" ] &&
85+ options . data &&
86+ options . type !== "GET"
87+ ) {
88+ options . headers [ "Content-Type" ] = "application/x-www-form-urlencoded" ;
89+ }
90+
91+ if ( options . dataType ) {
92+ options . headers [ "Accept" ] =
93+ MIME_TYPES [ options . dataType ] || options . dataType ;
94+ }
11095
111- for ( var key in options . headers ) {
112- var value = options . headers [ key ] ;
113- xhr . setRequestHeader ( key , value ) ;
96+ for ( var key in options . headers ) {
97+ var value = options . headers [ key ] ;
98+ xhr . setRequestHeader ( key , value ) ;
99+ }
114100 }
115- } ;
116101
117- var onComplete = function ( xhr , options ) {
118- if ( 200 <= xhr . status && xhr . status < 300 ) {
119- let response ;
120- if ( ( response = parseResponse ( xhr , options ) ) != null ) {
121- onSuccess ( response , xhr , options ) ;
102+ function onComplete ( xhr , options ) {
103+ if ( 200 <= xhr . status && xhr . status < 300 ) {
104+ let response ;
105+ if ( ( response = parseResponse ( xhr , options ) ) != null ) {
106+ onSuccess ( response , xhr , options ) ;
107+ } else {
108+ onError ( "invalid" , xhr , options ) ;
109+ }
122110 } else {
123- onError ( "invalid " , xhr , options ) ;
111+ onError ( "error " , xhr , options ) ;
124112 }
125- } else {
126- onError ( "error" , xhr , options ) ;
127113 }
128- } ;
129114
130- var onSuccess = function ( response , xhr , options ) {
131- if ( options . success != null ) {
132- options . success . call ( options . context , response , xhr , options ) ;
115+ function onSuccess ( response , xhr , options ) {
116+ if ( options . success != null ) {
117+ options . success . call ( options . context , response , xhr , options ) ;
118+ }
133119 }
134- } ;
135120
136- var onError = function ( type , xhr , options ) {
137- if ( options . error != null ) {
138- options . error . call ( options . context , type , xhr , options ) ;
121+ function onError ( type , xhr , options ) {
122+ if ( options . error != null ) {
123+ options . error . call ( options . context , type , xhr , options ) ;
124+ }
139125 }
140- } ;
141126
142- var onTimeout = function ( xhr , options ) {
143- xhr . abort ( ) ;
144- onError ( "timeout" , xhr , options ) ;
145- } ;
127+ function onTimeout ( xhr , options ) {
128+ xhr . abort ( ) ;
129+ onError ( "timeout" , xhr , options ) ;
130+ }
146131
147- var abort = function ( xhr ) {
148- clearTimeout ( xhr . timer ) ;
149- xhr . onreadystatechange = null ;
150- xhr . abort ( ) ;
151- } ;
132+ function abort ( xhr ) {
133+ clearTimeout ( xhr . timer ) ;
134+ xhr . onreadystatechange = null ;
135+ xhr . abort ( ) ;
136+ }
152137
153- var parseResponse = function ( xhr , options ) {
154- if ( options . dataType === "json" ) {
155- return parseJSON ( xhr . responseText ) ;
156- } else {
157- return xhr . responseText ;
138+ function parseResponse ( xhr , options ) {
139+ if ( options . dataType === "json" ) {
140+ return parseJSON ( xhr . responseText ) ;
141+ } else {
142+ return xhr . responseText ;
143+ }
158144 }
159- } ;
160145
161- var parseJSON = function ( json ) {
162- try {
163- return JSON . parse ( json ) ;
164- } catch ( error ) { }
146+ function parseJSON ( json ) {
147+ try {
148+ return JSON . parse ( json ) ;
149+ } catch ( error ) { }
150+ }
151+ }
152+
153+ ajax . defaults = {
154+ async : true ,
155+ dataType : "json" ,
156+ timeout : 30 ,
157+ type : "GET" ,
158+ // contentType
159+ // context
160+ // data
161+ // error
162+ // headers
163+ // progress
164+ // success
165+ // url
165166} ;
0 commit comments