Skip to content

Commit b5bbfd6

Browse files
author
Chasen Le Hara
committed
2.2.2
1 parent 3076536 commit b5bbfd6

File tree

3 files changed

+784
-0
lines changed

3 files changed

+784
-0
lines changed

dist/amd/can-ajax.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*can-ajax@2.2.1#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+
if (!o.contentType) {
109+
o.contentType = o.type.toUpperCase() === 'GET' ? contentTypes.form : contentTypes.json;
110+
}
111+
if (o.crossDomain == null) {
112+
try {
113+
requestUrl = parseURI(o.url);
114+
o.crossDomain = !!(requestUrl.protocol && requestUrl.protocol !== originUrl.protocol || requestUrl.host && requestUrl.host !== originUrl.host);
115+
} catch (e) {
116+
o.crossDomain = true;
117+
}
118+
}
119+
if (o.timeout) {
120+
timer = setTimeout(function () {
121+
xhr.abort();
122+
if (o.timeoutFn) {
123+
o.timeoutFn(o.url);
124+
}
125+
}, o.timeout);
126+
}
127+
xhr.onreadystatechange = function () {
128+
try {
129+
if (xhr.readyState === 4) {
130+
if (timer) {
131+
clearTimeout(timer);
132+
}
133+
if (xhr.status < 300) {
134+
if (o.success) {
135+
o.success(_xhrResp(xhr, o));
136+
}
137+
} else if (o.error) {
138+
o.error(xhr, xhr.status, xhr.statusText);
139+
}
140+
if (o.complete) {
141+
o.complete(xhr, xhr.statusText);
142+
}
143+
if (xhr.status >= 200 && xhr.status < 300) {
144+
deferred.resolve(_xhrResp(xhr, o));
145+
} else {
146+
deferred.reject(_xhrResp(xhr, o));
147+
}
148+
} else if (o.progress) {
149+
o.progress(++n);
150+
}
151+
} catch (e) {
152+
deferred.reject(e);
153+
}
154+
};
155+
var url = o.url, data = null, type = o.type.toUpperCase();
156+
var isJsonContentType = o.contentType === contentTypes.json;
157+
var isPost = type === 'POST' || type === 'PUT';
158+
if (!isPost && o.data) {
159+
url += '?' + (isJsonContentType ? JSON.stringify(o.data) : param(o.data));
160+
}
161+
xhr.open(type, url, true);
162+
var isSimpleCors = o.crossDomain && [
163+
'GET',
164+
'POST',
165+
'HEAD'
166+
].indexOf(type) !== -1;
167+
isFormData = typeof FormData !== 'undefined' && o.data instanceof FormData;
168+
if (isPost) {
169+
if (isFormData) {
170+
data = o.data;
171+
} else {
172+
data = isJsonContentType && !isSimpleCors ? typeof o.data === 'object' ? JSON.stringify(o.data) : o.data : param(o.data);
173+
}
174+
var setContentType = isJsonContentType && !isSimpleCors ? 'application/json' : 'application/x-www-form-urlencoded';
175+
xhr.setRequestHeader('Content-Type', setContentType);
176+
} else {
177+
xhr.setRequestHeader('Content-Type', o.contentType);
178+
}
179+
if (!isSimpleCors) {
180+
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
181+
}
182+
if (o.xhrFields) {
183+
for (var f in o.xhrFields) {
184+
xhr[f] = o.xhrFields[f];
185+
}
186+
}
187+
function send() {
188+
if (!isAborted) {
189+
xhr.send(data);
190+
}
191+
}
192+
if (o.beforeSend) {
193+
const result = o.beforeSend.call(o, xhr, o);
194+
if (canReflect.isPromise(result)) {
195+
result.then(send).catch(deferred.reject);
196+
return promise;
197+
}
198+
}
199+
send();
200+
return promise;
201+
}
202+
module.exports = namespace.ajax = ajax;
203+
module.exports.ajaxSetup = function (o) {
204+
globalSettings = o || {};
205+
};
206+
}(function () {
207+
return this;
208+
}(), require, exports, module));
209+
});

0 commit comments

Comments
 (0)