-
Notifications
You must be signed in to change notification settings - Fork 7
/
http.js
38 lines (33 loc) · 903 Bytes
/
http.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
var http = (function() {
var request = function(method, url, data) {
return new Promise(function(resolve, reject) {
var sendingData = (data !== void 0);
var r = new XMLHttpRequest();
r.open(method, url);
r.setRequestHeader('Accept', 'application/json');
if (sendingData) {
r.setRequestHeader('Content-Type', 'application/json');
}
r.onload = function() {
if (r.status >= 200 && r.status < 300) {
resolve(JSON.parse(r.response));
} else {
reject(Error(r.statusText));
}
};
r.onerror = function() {
reject(Error("Network error"));
};
if (sendingData) {
r.send(JSON.stringify(data));
} else {
r.send();
}
});
};
return {
get: function(url) {
return request('GET', url);
}
};
})();