ajax的get,post ,封装

 

let ajax = new Object();

ajax.get = function(url,fn){

  //创建ajax对象

  let xhr = new XMLHttpRequest();

  //与服务器建立连接

  xhr.open('GET',url,true);

  //发送请求

  xhr.send();

  //等待返回的数据

  onreadystatechange = function(){

    if(xhr.readyState === 4 && xhr.status === 200){

      if(typeof fn=== 'function'){

      fn(xhr.responseText);

      }

    }

  }

}

ajax.post = function(url,data,fn){

  var xhr = new XMLHttpRequest();

  xhr.open('OPST',url,true);

  xhr.send(data);

  onreadtstatechange = function(){

  if(xhr.readystate === 200 && xhr.status === 4){

    if(typeof fn === 'function'){

      fn(xhr.responseText);

      }

    }

  }

}

  

posted on 2019-01-08 20:35  阿垚  阅读(266)  评论(0编辑  收藏  举报