//用promise进行封装
function ajax(url) {
return new Promise(
(resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) { //结束并有返回值状态
if (xhr.status === 200) {
resolve(xhr.responseText)
}else{
reject("失败了呢!")
}
}
}
xhr.open("GET",url,true);
xhr.send(null);
}
)
}
ajax("./data1.json").then(res=>{
alert(`下载成功!${res}`);
}).catch(err=>{
alert(`${err}`);
})