meetandeat/webroot/js/utils.js

29 lines
577 B
JavaScript
Raw Normal View History

2019-03-22 16:35:16 +01:00
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');
}