Skip to content

Commit c2d50ed

Browse files
1.4.0
1 parent 02f05e3 commit c2d50ed

File tree

4 files changed

+9962
-0
lines changed

4 files changed

+9962
-0
lines changed

dist/amd/can-ajax.js

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

dist/cjs/can-ajax.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
var async = o.async !== false;
87+
if (!o.contentType) {
88+
o.contentType = o.type.toUpperCase() === 'GET' ? contentTypes.form : contentTypes.json;
89+
}
90+
if (o.crossDomain == null) {
91+
try {
92+
requestUrl = parseURI(o.url);
93+
o.crossDomain = !!(requestUrl.protocol && requestUrl.protocol !== originUrl.protocol || requestUrl.host && requestUrl.host !== originUrl.host);
94+
} catch (e) {
95+
o.crossDomain = true;
96+
}
97+
}
98+
if (o.timeout) {
99+
timer = setTimeout(function () {
100+
xhr.abort();
101+
if (o.timeoutFn) {
102+
o.timeoutFn(o.url);
103+
}
104+
}, o.timeout);
105+
}
106+
xhr.onreadystatechange = function () {
107+
try {
108+
if (xhr.readyState === 4) {
109+
if (timer) {
110+
clearTimeout(timer);
111+
}
112+
if (xhr.status < 300) {
113+
if (o.success) {
114+
o.success(_xhrResp(xhr, o));
115+
}
116+
} else if (o.error) {
117+
o.error(xhr, xhr.status, xhr.statusText);
118+
}
119+
if (o.complete) {
120+
o.complete(xhr, xhr.statusText);
121+
}
122+
if (xhr.status >= 200 && xhr.status < 300) {
123+
deferred.resolve(_xhrResp(xhr, o));
124+
} else {
125+
deferred.reject(xhr);
126+
}
127+
} else if (o.progress) {
128+
o.progress(++n);
129+
}
130+
} catch (e) {
131+
deferred.reject(e);
132+
}
133+
};
134+
var url = o.url, data = null, type = o.type.toUpperCase();
135+
var isJsonContentType = o.contentType === contentTypes.json;
136+
var isPost = type === 'POST' || type === 'PUT';
137+
if (!isPost && o.data) {
138+
url += '?' + (isJsonContentType ? JSON.stringify(o.data) : param(o.data));
139+
}
140+
xhr.open(type, url, async);
141+
var isSimpleCors = o.crossDomain && [
142+
'GET',
143+
'POST',
144+
'HEAD'
145+
].indexOf(type) !== -1;
146+
isFormData = typeof FormData !== 'undefined' && o.data instanceof FormData;
147+
if (isPost) {
148+
if (isFormData) {
149+
data = o.data;
150+
} else {
151+
data = isJsonContentType && !isSimpleCors ? typeof o.data === 'object' ? JSON.stringify(o.data) : o.data : param(o.data);
152+
}
153+
var setContentType = isJsonContentType && !isSimpleCors ? 'application/json' : 'application/x-www-form-urlencoded';
154+
xhr.setRequestHeader('Content-Type', setContentType);
155+
} else {
156+
xhr.setRequestHeader('Content-Type', o.contentType);
157+
}
158+
if (!isSimpleCors) {
159+
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
160+
}
161+
if (o.xhrFields) {
162+
for (var f in o.xhrFields) {
163+
xhr[f] = o.xhrFields[f];
164+
}
165+
}
166+
xhr.send(data);
167+
return promise;
168+
}
169+
module.exports = namespace.ajax = ajax;
170+
module.exports.ajaxSetup = function (o) {
171+
globalSettings = o || {};
172+
};

0 commit comments

Comments
 (0)