Skip to content

Commit e259caf

Browse files
Implement ajaxSetup for reverse compatibility with $.ajaxSetup and pass xhrFields to xhr object for withCredentials and other params
1 parent 227436e commit e259caf

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

can-ajax.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var Global = require("can-globals/global/global");
4-
var assign = require("can-assign");
4+
var deepAssign = require("can-util/js/deep-assign/deep-assign");
55
var namespace = require("can-namespace");
66
var parseURI = require('can-parse-uri');
77
var param = require("can-param");
@@ -52,6 +52,8 @@ var xhrs = [
5252
// used to check for Cross Domain requests
5353
var originUrl = parseURI(Global().location.href);
5454

55+
var globalSettings = {};
56+
5557
var makeXhr = function () {
5658
if (_xhrf != null) {
5759
return _xhrf();
@@ -92,7 +94,7 @@ var _xhrResp = function (xhr, options) {
9294
}
9395
};
9496

95-
module.exports = namespace.ajax = function (o) {
97+
function ajax(o) {
9698
var xhr = makeXhr(), timer, n = 0;
9799
var deferred = {};
98100
var promise = new Promise(function(resolve,reject){
@@ -105,13 +107,13 @@ module.exports = namespace.ajax = function (o) {
105107
xhr.abort();
106108
};
107109

108-
o = assign({
110+
o = deepAssign({
109111
userAgent: "XMLHttpRequest",
110112
lang: "en",
111113
type: "GET",
112114
data: null,
113115
dataType: "json"
114-
}, o);
116+
}, globalSettings, o);
115117

116118
// Set the default contentType
117119
if(!o.contentType) {
@@ -179,7 +181,7 @@ module.exports = namespace.ajax = function (o) {
179181

180182
// For CORS to send a "simple" request (to avoid a preflight check), the following methods are allowed: GET/POST/HEAD,
181183
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests
182-
184+
183185
var isSimpleCors = o.crossDomain && ['GET', 'POST', 'HEAD'].indexOf(type) !== -1;
184186

185187
if (isPost) {
@@ -200,6 +202,17 @@ module.exports = namespace.ajax = function (o) {
200202
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
201203
}
202204

205+
if (o.xhrFields) {
206+
for (var f in o.xhrFields) {
207+
xhr[f] = o.xhrFields[f];
208+
}
209+
}
210+
203211
xhr.send(data);
204212
return promise;
205213
};
214+
215+
module.exports = namespace.ajax = ajax
216+
module.exports.ajaxSetup = function (o) {
217+
globalSettings = o || {};
218+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
]
4747
},
4848
"dependencies": {
49-
"can-assign": "^1.0.0",
5049
"can-globals": "0.1.0",
5150
"can-namespace": "^1.0.0",
5251
"can-param": "^1.0.1",
53-
"can-parse-uri": "^1.0.0"
52+
"can-parse-uri": "^1.0.0",
53+
"can-util": "^3.10.8"
5454
},
5555
"devDependencies": {
5656
"can-make-map": "^1.0.0",

0 commit comments

Comments
 (0)