手写一个简易的ajax请求

function ajax(url){
  const p=new Promise((resolve,reject)=>{
    const xhr=XMLHttpRequest()

    xhr.open('GET','/data/test.json',true)

    xhr.onreadystatechange=function(){
      if(xhr.readyState===4){
        if(xhr.status===200){
          JSON.parse(xhr.responseText)
        }else if(xhr.status===404){

          reject(new Error('404 not found'))
        }
      }
    }

  });
  return p
}

猜你喜欢

转载自www.cnblogs.com/fanggood/p/12215886.html