手写AJAX

首先创建XMLHttpRequest对象,第二步调用open方法,第三步监听对象的onload和onerror事件,或用onreadystatechange,最后调用send方法。

完整版

var request = new XMLHttpRequest();
request.open('GET','/xxx',true);
request.onreadystatechange = ()=>{
  if(request.readyState === 4 && request.status === 200){
    console.log(request.resopnse)
  }
};
request.send();

简易版

var request = new XMLHttpRequest();
request.open('GET','/xxx',true);
request.onload =()=>{console.log('成功了')}//request.onerror =()=>{console.log('失败了')}
request.send();

你可能感兴趣的:(手写AJAX)