HomeMediacenter/js/BaseRequest.js
2020-04-04 19:44:31 +02:00

49 lines
1.5 KiB
JavaScript

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")
};
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(data);
}
static postData(url, data, callback) {
const xhttp = new XMLHttpRequest();
xhttp.open("POST",url,true);
xhttp.onload = function(){
if (this.readyState == 4 && this.status == 200) {
callback(xhttp.responseText);
}
}
xhttp.onerror = function () {
console.log("error")
};
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
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);
}
}