forked from 0x20/tab-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.js
44 lines (42 loc) · 1.46 KB
/
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
39
40
41
42
43
44
.pragma library
.import QuickPromise 1.0 as QP
function get(url) {
return new QP.Q.Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
console.log("XHR created for " + url);
xhr.onreadystatechange = (function(myxhr) {
return function() {
if (myxhr.readyState === 4) {
if (myxhr.status !== 200) {
reject(myxhr);
} else {
resolve(myxhr);
}
//console.log("XHR response for " + url + ": " + myxhr.status + " - " + myxhr.statusText);
}
};
})(xhr);
xhr.open('GET', url, true);
xhr.send();
})
}
function post(url, body) {
return new QP.Q.Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = (function(myxhr) {
return function() {
if (myxhr.readyState === 4) {
if (myxhr.status !== 200) {
reject(myxhr);
} else {
resolve(myxhr);
}
//console.log("XHR response for " + url + ": " + myxhr.status + " - " + myxhr.statusText);
}
};
})(xhr);
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(body));
})
}