43 lines
1012 B
JavaScript
43 lines
1012 B
JavaScript
class Http {
|
|
|
|
constructor() {
|
|
this.xhr = new XMLHttpRequest();
|
|
}
|
|
|
|
|
|
get(params) {
|
|
var par = ""
|
|
for (const key in params.data) {
|
|
par += `&${key}=${params.data[key]}`
|
|
}
|
|
par = par.replace("&", "?")
|
|
this.xhr.open("GET", `${params.url}${par}`)
|
|
this.xhr.send()
|
|
|
|
this.ReadyState(params)
|
|
}
|
|
|
|
post(params) {
|
|
this.xhr.open("POST", `${params.url}`)
|
|
this.xhr.setRequestHeader('Content-Type', 'application/json');
|
|
this.xhr.send(JSON.stringify(params.data))
|
|
this.ReadyState(params)
|
|
}
|
|
|
|
|
|
ReadyState(params) {
|
|
this.xhr.onreadystatechange = () => {
|
|
if (this.xhr.readyState == 4) {
|
|
if (this.xhr.status == 200) {
|
|
var data = JSON.parse(this.xhr.response);
|
|
params.success(data)
|
|
} else {
|
|
params.error(this.xhr)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var http = new Http();
|