当前位置:首页 > Web开发 > 正文

function ajax(url){ const p=new Promise((resolve

2024-03-31 Web开发

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
}

手写一个简易的ajax请求

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31329.html