Skip to content

Commit 0d203b8

Browse files
committed
2.1.0
1 parent a36ffc1 commit 0d203b8

File tree

3 files changed

+739
-0
lines changed

3 files changed

+739
-0
lines changed

dist/amd/can-ajax.js

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

dist/cjs/can-ajax.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*can-ajax@2.0.2#can-ajax*/
2+
'use strict';
3+
var Global = require('can-globals/global/global');
4+
var canReflect = require('can-reflect');
5+
var namespace = require('can-namespace');
6+
var parseURI = require('can-parse-uri');
7+
var param = require('can-param');
8+
var xhrs = [
9+
function () {
10+
return new XMLHttpRequest();
11+
},
12+
function () {
13+
return new ActiveXObject('Microsoft.XMLHTTP');
14+
},
15+
function () {
16+
return new ActiveXObject('MSXML2.XMLHTTP.3.0');
17+
},
18+
function () {
19+
return new ActiveXObject('MSXML2.XMLHTTP');
20+
}
21+
], _xhrf = null;
22+
var originUrl = parseURI(Global().location.href);
23+
var globalSettings = {};
24+
var makeXhr = function () {
25+
if (_xhrf != null) {
26+
return _xhrf();
27+
}
28+
for (var i = 0, l = xhrs.length; i < l; i++) {
29+
try {
30+
var f = xhrs[i], req = f();
31+
if (req != null) {
32+
_xhrf = f;
33+
return req;
34+
}
35+
} catch (e) {
36+
continue;
37+
}
38+
}
39+
return function () {
40+
};
41+
};
42+
var contentTypes = {
43+
json: 'application/json',
44+
form: 'application/x-www-form-urlencoded'
45+
};
46+
var _xhrResp = function (xhr, options) {
47+
var type = options.dataType || xhr.getResponseHeader('Content-Type').split(';')[0];
48+
if (type && (xhr.responseText || xhr.responseXML)) {
49+
switch (type) {
50+
case 'text/xml':
51+
case 'xml':
52+
return xhr.responseXML;
53+
case 'text/json':
54+
case 'application/json':
55+
case 'text/javascript':
56+
case 'application/javascript':
57+
case 'application/x-javascript':
58+
case 'json':
59+
return xhr.responseText && JSON.parse(xhr.responseText);
60+
default:
61+
return xhr.responseText;
62+
}
63+
} else {
64+
return xhr;
65+
}
66+
};
67+
function ajax(o) {
68+
var xhr = makeXhr(), timer, n = 0;
69+
var deferred = {}, isFormData;
70+
var promise = new Promise(function (resolve, reject) {
71+
deferred.resolve = resolve;
72+
deferred.reject = reject;
73+
});
74+
var requestUrl;
75+
promise.abort = function () {
76+
xhr.abort();
77+
};
78+
o = [
79+
{
80+
userAgent: 'XMLHttpRequest',
81+
lang: 'en',
82+
type: 'GET',
83+
data: null,
84+
dataType: 'json'
85+
},
86+
globalSettings,
87+
o
88+
].reduce(function (a, b, i) {
89+
return canReflect.assignDeep(a, b);
90+
});
91+
if (!o.contentType) {
92+
o.contentType = o.type.toUpperCase() === 'GET' ? contentTypes.form : contentTypes.json;
93+
}
94+
if (o.crossDomain == null) {
95+
try {
96+
requestUrl = parseURI(o.url);
97+
o.crossDomain = !!(requestUrl.protocol && requestUrl.protocol !== originUrl.protocol || requestUrl.host && requestUrl.host !== originUrl.host);
98+
} catch (e) {
99+
o.crossDomain = true;
100+
}
101+
}
102+
if (o.timeout) {
103+
timer = setTimeout(function () {
104+
xhr.abort();
105+
if (o.timeoutFn) {
106+
o.timeoutFn(o.url);
107+
}
108+
}, o.timeout);
109+
}
110+
xhr.onreadystatechange = function () {
111+
try {
112+
if (xhr.readyState === 4) {
113+
if (timer) {
114+
clearTimeout(timer);
115+
}
116+
if (xhr.status < 300) {
117+
if (o.success) {
118+
o.success(_xhrResp(xhr, o));
119+
}
120+
} else if (o.error) {
121+
o.error(xhr, xhr.status, xhr.statusText);
122+
}
123+
if (o.complete) {
124+
o.complete(xhr, xhr.statusText);
125+
}
126+
if (xhr.status >= 200 && xhr.status < 300) {
127+
deferred.resolve(_xhrResp(xhr, o));
128+
} else {
129+
deferred.reject(_xhrResp(xhr, o));
130+
}
131+
} else if (o.progress) {
132+
o.progress(++n);
133+
}
134+
} catch (e) {
135+
deferred.reject(e);
136+
}
137+
};
138+
var url = o.url, data = null, type = o.type.toUpperCase();
139+
var isJsonContentType = o.contentType === contentTypes.json;
140+
var isPost = type === 'POST' || type === 'PUT';
141+
if (!isPost && o.data) {
142+
url += '?' + (isJsonContentType ? JSON.stringify(o.data) : param(o.data));
143+
}
144+
xhr.open(type, url);
145+
var isSimpleCors = o.crossDomain && [
146+
'GET',
147+
'POST',
148+
'HEAD'
149+
].indexOf(type) !== -1;
150+
isFormData = typeof FormData !== 'undefined' && o.data instanceof FormData;
151+
if (isPost) {
152+
if (isFormData) {
153+
data = o.data;
154+
} else {
155+
data = isJsonContentType && !isSimpleCors ? typeof o.data === 'object' ? JSON.stringify(o.data) : o.data : param(o.data);
156+
}
157+
var setContentType = isJsonContentType && !isSimpleCors ? 'application/json' : 'application/x-www-form-urlencoded';
158+
xhr.setRequestHeader('Content-Type', setContentType);
159+
} else {
160+
xhr.setRequestHeader('Content-Type', o.contentType);
161+
}
162+
if (!isSimpleCors) {
163+
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
164+
}
165+
if (o.beforeSend) {
166+
o.beforeSend.call(o, xhr, o);
167+
}
168+
if (o.xhrFields) {
169+
for (var f in o.xhrFields) {
170+
xhr[f] = o.xhrFields[f];
171+
}
172+
}
173+
xhr.send(data);
174+
return promise;
175+
}
176+
module.exports = namespace.ajax = ajax;
177+
module.exports.ajaxSetup = function (o) {
178+
globalSettings = o || {};
179+
};

0 commit comments

Comments
 (0)