42 lines
1.0 KiB
JavaScript
42 lines
1.0 KiB
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.readyState == 200) {
|
|
var data = JSON.parse(this.xhr.response);
|
|
promise.success(data)
|
|
} else {
|
|
params.error(this, xhr)
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
var http = new Http(); |