Skip to content

Commit 27dfc00

Browse files
committed
1.2.0
1 parent d8fc0f8 commit 27dfc00

File tree

3 files changed

+683
-0
lines changed

3 files changed

+683
-0
lines changed

dist/amd/can-ajax.js

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

dist/cjs/can-ajax.js

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

0 commit comments

Comments
 (0)