-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.js
47 lines (42 loc) · 1.01 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* ajax
*
* This module support for making HTTP requests using XHR.
*
* References:
* - [XMLHttpRequest](http://www.w3.org/TR/XMLHttpRequest/)
* - [XMLHttpRequest (WHATWG)](http://xhr.spec.whatwg.org/)
* - [MDN > DOM](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest)
*/
define(['exports',
'xhr',
'url'],
function(exports, xhr, uri) {
function request(url, method, cb) {
var headers;
if (typeof url == 'object') {
var opts = url;
cb = method;
method = opts.method || 'GET';
url = uri.format(opts);
headers = opts.headers;
} else if (typeof method == 'function') {
cb = method;
method = 'GET';
}
var req = xhr.request(url, method, cb);
if (headers) {
for (var name in headers) {
req.setHeader(name, headers[name]);
}
}
return req;
}
function get(url, cb) {
var req = request(url, cb);
req.send();
return req;
}
exports.request = request;
exports.get = get;
});