-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxhr-ajax.js
80 lines (70 loc) · 2.65 KB
/
xhr-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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* xhr-ajax.js Copyright (C) 2013 Craig Roberts (http://craig0990.co.uk)
*
* Licensed under the MIT License (http://mit-license.org)
*/
(function (root, factory) {
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else {
// Browser globals (root is window)
root.ajax = factory();
}
}(this, function () {
/**
* @param {String} url The URL to request
* @param {Object} [options] Options to apply:
* @param {String} [options.url] The URL to request
* @param {String} [options.method=GET] The HTTP method to use
* @param {Boolean} [options.async=true] Whether to call asynchronously
* @param {String} [options.user] Username for authenticated requests
* @param {String} [options.password] Password for authenticated requests
* @param {Object} [options.headers] HTTP Request headers, where keys are header fields
* @param {*} [options.data] Request data - passed directly to `send()`
*
* @returns {Promise}
*/
function ajax(url, options) {
if (typeof url == 'undefined') {
throw new TypeError('A URL is required for xhr-ajax');
}
if (typeof options === 'undefined' && typeof url === 'object' && url.url) {
options = url;
url = options.url;
} else if (typeof url !== 'string') {
throw new TypeError('Options must be an object for xhr-ajax');
}
options = options || {};
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(
options.method || 'GET',
url,
typeof options.async === 'undefined' ? true : options.async,
options.user,
options.password
);
Object.keys(options.headers || {}).forEach(function(name) {
xhr.setRequestHeader(name, options.headers[name]);
});
xhr.onreadystatechange = function() {
if (this.readyState !== 4) {
return;
}
if (this.status === 200) {
resolve(xhr);
} else {
reject(xhr);
}
};
xhr.send(options.data);
});
}
return ajax;
}));