1
+ /*can-ajax@2.0.2#can-ajax*/
2
+ define ( [
3
+ 'require' ,
4
+ 'exports' ,
5
+ 'module' ,
6
+ 'can-globals/global' ,
7
+ 'can-reflect' ,
8
+ 'can-namespace' ,
9
+ 'can-parse-uri' ,
10
+ 'can-param'
11
+ ] , function ( require , exports , module ) {
12
+ ( function ( global , require , exports , module ) {
13
+ 'use strict' ;
14
+ var Global = require ( 'can-globals/global' ) ;
15
+ var canReflect = require ( 'can-reflect' ) ;
16
+ var namespace = require ( 'can-namespace' ) ;
17
+ var parseURI = require ( 'can-parse-uri' ) ;
18
+ var param = require ( 'can-param' ) ;
19
+ var xhrs = [
20
+ function ( ) {
21
+ return new XMLHttpRequest ( ) ;
22
+ } ,
23
+ function ( ) {
24
+ return new ActiveXObject ( 'Microsoft.XMLHTTP' ) ;
25
+ } ,
26
+ function ( ) {
27
+ return new ActiveXObject ( 'MSXML2.XMLHTTP.3.0' ) ;
28
+ } ,
29
+ function ( ) {
30
+ return new ActiveXObject ( 'MSXML2.XMLHTTP' ) ;
31
+ }
32
+ ] , _xhrf = null ;
33
+ var originUrl = parseURI ( Global ( ) . location . href ) ;
34
+ var globalSettings = { } ;
35
+ var makeXhr = function ( ) {
36
+ if ( _xhrf != null ) {
37
+ return _xhrf ( ) ;
38
+ }
39
+ for ( var i = 0 , l = xhrs . length ; i < l ; i ++ ) {
40
+ try {
41
+ var f = xhrs [ i ] , req = f ( ) ;
42
+ if ( req != null ) {
43
+ _xhrf = f ;
44
+ return req ;
45
+ }
46
+ } catch ( e ) {
47
+ continue ;
48
+ }
49
+ }
50
+ return function ( ) {
51
+ } ;
52
+ } ;
53
+ var contentTypes = {
54
+ json : 'application/json' ,
55
+ form : 'application/x-www-form-urlencoded'
56
+ } ;
57
+ var _xhrResp = function ( xhr , options ) {
58
+ var type = options . dataType || xhr . getResponseHeader ( 'Content-Type' ) . split ( ';' ) [ 0 ] ;
59
+ if ( type && ( xhr . responseText || xhr . responseXML ) ) {
60
+ switch ( type ) {
61
+ case 'text/xml' :
62
+ case 'xml' :
63
+ return xhr . responseXML ;
64
+ case 'text/json' :
65
+ case 'application/json' :
66
+ case 'text/javascript' :
67
+ case 'application/javascript' :
68
+ case 'application/x-javascript' :
69
+ case 'json' :
70
+ return xhr . responseText && JSON . parse ( xhr . responseText ) ;
71
+ default :
72
+ return xhr . responseText ;
73
+ }
74
+ } else {
75
+ return xhr ;
76
+ }
77
+ } ;
78
+ function ajax ( o ) {
79
+ var xhr = makeXhr ( ) , timer , n = 0 ;
80
+ var deferred = { } , isFormData ;
81
+ var promise = new Promise ( function ( resolve , reject ) {
82
+ deferred . resolve = resolve ;
83
+ deferred . reject = reject ;
84
+ } ) ;
85
+ var requestUrl ;
86
+ promise . abort = function ( ) {
87
+ xhr . abort ( ) ;
88
+ } ;
89
+ o = [
90
+ {
91
+ userAgent : 'XMLHttpRequest' ,
92
+ lang : 'en' ,
93
+ type : 'GET' ,
94
+ data : null ,
95
+ dataType : 'json'
96
+ } ,
97
+ globalSettings ,
98
+ o
99
+ ] . reduce ( function ( a , b , i ) {
100
+ return canReflect . assignDeep ( a , b ) ;
101
+ } ) ;
102
+ if ( ! o . contentType ) {
103
+ o . contentType = o . type . toUpperCase ( ) === 'GET' ? contentTypes . form : contentTypes . json ;
104
+ }
105
+ if ( o . crossDomain == null ) {
106
+ try {
107
+ requestUrl = parseURI ( o . url ) ;
108
+ o . crossDomain = ! ! ( requestUrl . protocol && requestUrl . protocol !== originUrl . protocol || requestUrl . host && requestUrl . host !== originUrl . host ) ;
109
+ } catch ( e ) {
110
+ o . crossDomain = true ;
111
+ }
112
+ }
113
+ if ( o . timeout ) {
114
+ timer = setTimeout ( function ( ) {
115
+ xhr . abort ( ) ;
116
+ if ( o . timeoutFn ) {
117
+ o . timeoutFn ( o . url ) ;
118
+ }
119
+ } , o . timeout ) ;
120
+ }
121
+ xhr . onreadystatechange = function ( ) {
122
+ try {
123
+ if ( xhr . readyState === 4 ) {
124
+ if ( timer ) {
125
+ clearTimeout ( timer ) ;
126
+ }
127
+ if ( xhr . status < 300 ) {
128
+ if ( o . success ) {
129
+ o . success ( _xhrResp ( xhr , o ) ) ;
130
+ }
131
+ } else if ( o . error ) {
132
+ o . error ( xhr , xhr . status , xhr . statusText ) ;
133
+ }
134
+ if ( o . complete ) {
135
+ o . complete ( xhr , xhr . statusText ) ;
136
+ }
137
+ if ( xhr . status >= 200 && xhr . status < 300 ) {
138
+ deferred . resolve ( _xhrResp ( xhr , o ) ) ;
139
+ } else {
140
+ deferred . reject ( _xhrResp ( xhr , o ) ) ;
141
+ }
142
+ } else if ( o . progress ) {
143
+ o . progress ( ++ n ) ;
144
+ }
145
+ } catch ( e ) {
146
+ deferred . reject ( e ) ;
147
+ }
148
+ } ;
149
+ var url = o . url , data = null , type = o . type . toUpperCase ( ) ;
150
+ var isJsonContentType = o . contentType === contentTypes . json ;
151
+ var isPost = type === 'POST' || type === 'PUT' ;
152
+ if ( ! isPost && o . data ) {
153
+ url += '?' + ( isJsonContentType ? JSON . stringify ( o . data ) : param ( o . data ) ) ;
154
+ }
155
+ xhr . open ( type , url ) ;
156
+ var isSimpleCors = o . crossDomain && [
157
+ 'GET' ,
158
+ 'POST' ,
159
+ 'HEAD'
160
+ ] . indexOf ( type ) !== - 1 ;
161
+ isFormData = typeof FormData !== 'undefined' && o . data instanceof FormData ;
162
+ if ( isPost ) {
163
+ if ( isFormData ) {
164
+ data = o . data ;
165
+ } else {
166
+ data = isJsonContentType && ! isSimpleCors ? typeof o . data === 'object' ? JSON . stringify ( o . data ) : o . data : param ( o . data ) ;
167
+ }
168
+ var setContentType = isJsonContentType && ! isSimpleCors ? 'application/json' : 'application/x-www-form-urlencoded' ;
169
+ xhr . setRequestHeader ( 'Content-Type' , setContentType ) ;
170
+ } else {
171
+ xhr . setRequestHeader ( 'Content-Type' , o . contentType ) ;
172
+ }
173
+ if ( ! isSimpleCors ) {
174
+ xhr . setRequestHeader ( 'X-Requested-With' , 'XMLHttpRequest' ) ;
175
+ }
176
+ if ( o . beforeSend ) {
177
+ o . beforeSend . call ( o , xhr , o ) ;
178
+ }
179
+ if ( o . xhrFields ) {
180
+ for ( var f in o . xhrFields ) {
181
+ xhr [ f ] = o . xhrFields [ f ] ;
182
+ }
183
+ }
184
+ xhr . send ( data ) ;
185
+ return promise ;
186
+ }
187
+ module . exports = namespace . ajax = ajax ;
188
+ module . exports . ajaxSetup = function ( o ) {
189
+ globalSettings = o || { } ;
190
+ } ;
191
+ } ( function ( ) {
192
+ return this ;
193
+ } ( ) , require , exports , module ) ) ;
194
+ } ) ;
0 commit comments