Skip to content

Commit f0a5b87

Browse files
committed
2.4.3
1 parent a88924a commit f0a5b87

File tree

3 files changed

+787
-0
lines changed

3 files changed

+787
-0
lines changed

dist/amd/can-ajax.js

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

0 commit comments

Comments
 (0)