HomeMediacenter/js/BaseRequest.js

34 lines
998 B
JavaScript
Raw Normal View History

2020-03-31 09:44:52 +00:00
class Req {
static post(url, data, callback) {
const xhttp = new XMLHttpRequest();
xhttp.open("POST",url,true);
xhttp.onload = function(){
if (this.readyState == 4 && this.status == 200) {
callback(JSON.parse(xhttp.responseText));
}
}
xhttp.onerror = function () {
console.log("error")
};
2020-03-31 17:22:08 +00:00
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
2020-03-31 09:44:52 +00:00
xhttp.send(data);
}
static get(url, callback) {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
callback(JSON.parse(xhttp.responseText));
}else{
console.log("err")
}
}
xhttp.open("GET",url,true);
xhttp.send();
}
static ready(callback){
document.addEventListener('DOMContentLoaded', callback);
}
}