//发送Ajax请求,返回的结果是promise对象
function sendAjax(url){
return new Promise((resolve,reject) => {
//1.创建对象
const x = new XMLHttpRequest( );
//2.初始化 Get请求
x.open(‘GET’,URL);
//3.发送
x.send( );
//4.事件绑定
x.onreadystatechange = function( ){
if(x.readyState ===4){
//状态判断
if(x.status >= 200 && x.status < 300){
//成功啦
resolve(x.response)
}else{
reject(x.respone)
}
}
}
})
}
//promise then 测试
const result = sendAJAX(请求地址).then(value => {
console.log(value)
},reason => {})
//async 与 await 测试
async function main( ){
//发送Ajax请求
let result = await sendAJAX(请求地址)
console.log(result)
}