Files
ClassContent/历届学生/fontendseven/每天课程/2021-11-26/js/http.js
2024-09-27 02:06:13 +08:00

39 lines
970 B
JavaScript

class HTTP {
constructor() {
this.xhr = new XMLHttpRequest();
}
get(option) {
return new Promise((success, error) => {
var GetParams = ""
for (const key in option.data) {
GetParams += `&${key}=${option.data[key]}`
}
GetParams = GetParams.replace("&", "?")
option.url += GetParams;
this.xhr.open("GET", config.checkUrl() + option.url)
this.xhr.send()
this.onreadystatechange(success, error)
})
}
post() { }
onreadystatechange(success, error) {
this.xhr.onreadystatechange = () => {
if (this.xhr.readyState == 4) {
if (this.xhr.status == 200) {
success(JSON.parse(this.xhr.responseText));
} else {
console.log("暂无数据");
error(this.xhr)
}
}
}
}
}