Files
ClassContent/历届学生/front-end-team-10/项目练习/学生项目/韩孟雪/2023-3-3/ajaxV1.js
2024-09-27 02:06:13 +08:00

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();