Files
2024-09-27 02:06:13 +08:00

43 lines
1.2 KiB
JavaScript

class Ajax {
//url接口
http = new XMLHttpRequest();
constructor() {
}
get(option) {
var params = ""//留空进行处理
for (const key in option.data) {
console.log(key);
console.log(option.data[key]);
params += `&${key}=${option.data[key]}`//拼接字符串
}
params = params.replace("&", "?");//替换字符串
option.url = option.url + params;//拼接字符串
this.http.open("GET", option.url);//发送请求
this.http.send()//发送请求
this.onreadystatechange(option);//传方法
console.log(option.url)
}
post(option) {
this.http.open("POST", option.url);
this.http.setRequestHeader('Content-Type', 'application/josn');
this.http.send(JSON.stringify(option.data))
this.onreadystatechange(option)
}
onreadystatechange(callback){
this.http.onreadystatechange = () =>{
if (this.http.readyState == 4) {
if (this.http.status == 200) {
callback.success(JSON.parse(this.http.response))
} else {
callback.error(this.http)
}
}
}
}
}
var ajax = new Ajax()