简单记录一下原生ajax

面试老忘记,代码如下

function ajax() {
  var xmlHttpRequest = null; //定义XMLHttp对象的容器

  if(window.XMLHttpRequest) {//针对FireFox、Mozllar、Opera、Safari、IE7、IE8
       xmlHttpRequest = new XMLHttpRequest();  
  }    
  else if(window.ActiveXObject) {//针对IE6、IE5
       xmlHttpRequest = new ActiveXObject('Mircrosoft XMLHTTP')
  } 
  if(xmlHttpRequest != null) {
        xmlHttpRequest.open('GET', url, true);//打开对应的Url
        xmlHttpRequest.onreadystateChange = RequestCallBack;//如果请求过程顺利成功,则执行回调函数
       xmlHttpRequest.send(null);
  }
}

function RequestCallBack() {
   if(xmlHttpRequest.readyState == 4){//已成功接收来自服务器端的数据(不知数据如何)
      if(xmlHttpRequest.status == 200){//服务器端返回成功的数据
          //doing something
      }
  }
}

  

posted @ 2017-09-11 16:30  落落月  阅读(131)  评论(0编辑  收藏  举报