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