function ajax(url) {
return new Promise (function(resolve, reject){
let xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function () {
if(xhr.readyState == 4){//4代表解析完成
if(xhr.status === 200){
resolve(req.responseText);
}else{
reject(new Error(req.statusText));
}
}
}
xhr.send();
})
}
ajax("http://localhost:3000/test").then(res=>{
console.log(res.data);
}).catch(err=>{
})
js-----手写一个ajax
最新推荐文章于 2023-09-05 21:56:43 发布