1
+ /*can-ajax@1.4.0#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 ;
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
+ switch ( options . dataType || xhr . getResponseHeader ( 'Content-Type' ) . split ( ';' ) [ 0 ] ) {
59
+ case 'text/xml' :
60
+ case 'xml' :
61
+ return xhr . responseXML ;
62
+ case 'text/json' :
63
+ case 'application/json' :
64
+ case 'text/javascript' :
65
+ case 'application/javascript' :
66
+ case 'application/x-javascript' :
67
+ case 'json' :
68
+ return xhr . responseText && JSON . parse ( xhr . responseText ) ;
69
+ default :
70
+ return xhr . responseText ;
71
+ }
72
+ } ;
73
+ function ajax ( o ) {
74
+ if ( ! originUrl ) {
75
+ originUrl = parseURI ( Global ( ) . location . href ) ;
76
+ }
77
+ var xhr = makeXhr ( ) , timer , n = 0 ;
78
+ var deferred = { } , isFormData ;
79
+ var promise = new Promise ( function ( resolve , reject ) {
80
+ deferred . resolve = resolve ;
81
+ deferred . reject = reject ;
82
+ } ) ;
83
+ var requestUrl ;
84
+ promise . abort = function ( ) {
85
+ xhr . abort ( ) ;
86
+ } ;
87
+ o = [
88
+ {
89
+ userAgent : 'XMLHttpRequest' ,
90
+ lang : 'en' ,
91
+ type : 'GET' ,
92
+ data : null ,
93
+ dataType : 'json'
94
+ } ,
95
+ globalSettings ,
96
+ o
97
+ ] . reduce ( function ( a , b , i ) {
98
+ return canReflect . assignDeep ( a , b ) ;
99
+ } ) ;
100
+ var async = o . async !== false ;
101
+ if ( ! o . contentType ) {
102
+ o . contentType = o . type . toUpperCase ( ) === 'GET' ? contentTypes . form : contentTypes . json ;
103
+ }
104
+ if ( o . crossDomain == null ) {
105
+ try {
106
+ requestUrl = parseURI ( o . url ) ;
107
+ o . crossDomain = ! ! ( requestUrl . protocol && requestUrl . protocol !== originUrl . protocol || requestUrl . host && requestUrl . host !== originUrl . host ) ;
108
+ } catch ( e ) {
109
+ o . crossDomain = true ;
110
+ }
111
+ }
112
+ if ( o . timeout ) {
113
+ timer = setTimeout ( function ( ) {
114
+ xhr . abort ( ) ;
115
+ if ( o . timeoutFn ) {
116
+ o . timeoutFn ( o . url ) ;
117
+ }
118
+ } , o . timeout ) ;
119
+ }
120
+ xhr . onreadystatechange = function ( ) {
121
+ try {
122
+ if ( xhr . readyState === 4 ) {
123
+ if ( timer ) {
124
+ clearTimeout ( timer ) ;
125
+ }
126
+ if ( xhr . status < 300 ) {
127
+ if ( o . success ) {
128
+ o . success ( _xhrResp ( xhr , o ) ) ;
129
+ }
130
+ } else if ( o . error ) {
131
+ o . error ( xhr , xhr . status , xhr . statusText ) ;
132
+ }
133
+ if ( o . complete ) {
134
+ o . complete ( xhr , xhr . statusText ) ;
135
+ }
136
+ if ( xhr . status >= 200 && xhr . status < 300 ) {
137
+ deferred . resolve ( _xhrResp ( xhr , o ) ) ;
138
+ } else {
139
+ deferred . reject ( xhr ) ;
140
+ }
141
+ } else if ( o . progress ) {
142
+ o . progress ( ++ n ) ;
143
+ }
144
+ } catch ( e ) {
145
+ deferred . reject ( e ) ;
146
+ }
147
+ } ;
148
+ var url = o . url , data = null , type = o . type . toUpperCase ( ) ;
149
+ var isJsonContentType = o . contentType === contentTypes . json ;
150
+ var isPost = type === 'POST' || type === 'PUT' ;
151
+ if ( ! isPost && o . data ) {
152
+ url += '?' + ( isJsonContentType ? JSON . stringify ( o . data ) : param ( o . data ) ) ;
153
+ }
154
+ xhr . open ( type , url , async ) ;
155
+ var isSimpleCors = o . crossDomain && [
156
+ 'GET' ,
157
+ 'POST' ,
158
+ 'HEAD'
159
+ ] . indexOf ( type ) !== - 1 ;
160
+ isFormData = typeof FormData !== 'undefined' && o . data instanceof FormData ;
161
+ if ( isPost ) {
162
+ if ( isFormData ) {
163
+ data = o . data ;
164
+ } else {
165
+ data = isJsonContentType && ! isSimpleCors ? typeof o . data === 'object' ? JSON . stringify ( o . data ) : o . data : param ( o . data ) ;
166
+ }
167
+ var setContentType = isJsonContentType && ! isSimpleCors ? 'application/json' : 'application/x-www-form-urlencoded' ;
168
+ xhr . setRequestHeader ( 'Content-Type' , setContentType ) ;
169
+ } else {
170
+ xhr . setRequestHeader ( 'Content-Type' , o . contentType ) ;
171
+ }
172
+ if ( ! isSimpleCors ) {
173
+ xhr . setRequestHeader ( 'X-Requested-With' , 'XMLHttpRequest' ) ;
174
+ }
175
+ if ( o . xhrFields ) {
176
+ for ( var f in o . xhrFields ) {
177
+ xhr [ f ] = o . xhrFields [ f ] ;
178
+ }
179
+ }
180
+ xhr . send ( data ) ;
181
+ return promise ;
182
+ }
183
+ module . exports = namespace . ajax = ajax ;
184
+ module . exports . ajaxSetup = function ( o ) {
185
+ globalSettings = o || { } ;
186
+ } ;
187
+ } ( function ( ) {
188
+ return this ;
189
+ } ( ) , require , exports , module ) ) ;
190
+ } ) ;
0 commit comments