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