直接上代码(☄⊙ω⊙)☄
funtion newAjax(url){
return new Promise((resolve,reject)=>{
let xhr = new XMLHttpRequest()
xhr.open('GET',url,false)
xhr.onreadystatechange = function(){
if(xhr.readystate !== 4) return
if(xhr.status === 200 || xhr.status === 304){
resolve(xhr.responseText)
}else{
reject(new Error(xhr.responseText))
}
}
xhr.send()
})
}
newAjax(url).then(res=>{
console.log('调用结果',res)
},err=>{
console.log('报错了')
})