1
1
'use strict' ;
2
2
3
3
var Global = require ( "can-globals/global/global" ) ;
4
- var assign = require ( "can-assign" ) ;
4
+ var deepAssign = require ( "can-util/js/deep-assign/deep -assign" ) ;
5
5
var namespace = require ( "can-namespace" ) ;
6
6
var parseURI = require ( 'can-parse-uri' ) ;
7
7
var param = require ( "can-param" ) ;
@@ -52,6 +52,8 @@ var xhrs = [
52
52
// used to check for Cross Domain requests
53
53
var originUrl = parseURI ( Global ( ) . location . href ) ;
54
54
55
+ var globalSettings = { } ;
56
+
55
57
var makeXhr = function ( ) {
56
58
if ( _xhrf != null ) {
57
59
return _xhrf ( ) ;
@@ -92,7 +94,7 @@ var _xhrResp = function (xhr, options) {
92
94
}
93
95
} ;
94
96
95
- module . exports = namespace . ajax = function ( o ) {
97
+ function ajax ( o ) {
96
98
var xhr = makeXhr ( ) , timer , n = 0 ;
97
99
var deferred = { } ;
98
100
var promise = new Promise ( function ( resolve , reject ) {
@@ -105,13 +107,13 @@ module.exports = namespace.ajax = function (o) {
105
107
xhr . abort ( ) ;
106
108
} ;
107
109
108
- o = assign ( {
110
+ o = deepAssign ( {
109
111
userAgent : "XMLHttpRequest" ,
110
112
lang : "en" ,
111
113
type : "GET" ,
112
114
data : null ,
113
115
dataType : "json"
114
- } , o ) ;
116
+ } , globalSettings , o ) ;
115
117
116
118
// Set the default contentType
117
119
if ( ! o . contentType ) {
@@ -179,7 +181,7 @@ module.exports = namespace.ajax = function (o) {
179
181
180
182
// For CORS to send a "simple" request (to avoid a preflight check), the following methods are allowed: GET/POST/HEAD,
181
183
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests
182
-
184
+
183
185
var isSimpleCors = o . crossDomain && [ 'GET' , 'POST' , 'HEAD' ] . indexOf ( type ) !== - 1 ;
184
186
185
187
if ( isPost ) {
@@ -200,6 +202,17 @@ module.exports = namespace.ajax = function (o) {
200
202
xhr . setRequestHeader ( "X-Requested-With" , "XMLHttpRequest" ) ;
201
203
}
202
204
205
+ if ( o . xhrFields ) {
206
+ for ( var f in o . xhrFields ) {
207
+ xhr [ f ] = o . xhrFields [ f ] ;
208
+ }
209
+ }
210
+
203
211
xhr . send ( data ) ;
204
212
return promise ;
205
213
} ;
214
+
215
+ module . exports = namespace . ajax = ajax
216
+ module . exports . ajaxSetup = function ( o ) {
217
+ globalSettings = o || { } ;
218
+ }
0 commit comments