Skip to content

Commit cb765c6

Browse files
committed
1.4.1
1 parent 138fd52 commit cb765c6

File tree

3 files changed

+727
-0
lines changed

3 files changed

+727
-0
lines changed

dist/amd/can-ajax.js

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

dist/cjs/can-ajax.js

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

0 commit comments

Comments
 (0)