29 lines
577 B
JavaScript
29 lines
577 B
JavaScript
function http(url,type,data) {
|
|
return new Promise(function (resolve, reject) {
|
|
var req = new XMLHttpRequest();
|
|
req.open(type, url);
|
|
|
|
req.onload = function onload() {
|
|
if (req.status === 200) {
|
|
resolve(req.response);
|
|
} else {
|
|
reject(Error(req.response));
|
|
}
|
|
};
|
|
|
|
req.onerror = function onerror() {
|
|
reject(Error('Network Error'));
|
|
};
|
|
req.send(data);
|
|
});
|
|
}
|
|
function httpJSON(url, type, data) {
|
|
return http(url, type, data).then(JSON.parse);
|
|
}
|
|
function get(url) {
|
|
return http(url,'GET');
|
|
}
|
|
function getJSON(url) {
|
|
return httpJSON(url, 'GET');
|
|
}
|