AJAX底层实现机制,通过XMLHTTPRequest对象

下面是区别IE6、IE7、IE8之间的方法: 

var isIE=!!window.ActiveXObject; 
var isIE6=isIE&&!window.XMLHttpRequest; 
var isIE8=isIE&&!!document.documentMode; 
var isIE7=isIE&&!isIE6&&!isIE8; 
if (isIE){ 
if (isIE6){ 
alert(”ie6″); 
}else if (isIE8){ 
alert(”ie8″); 
}else if (isIE7){ 
alert(”ie7″); 

function getXmlHttpRequest(){

     var  xhr = null;

    if  (window.ActiveXObject) {

           xhr = new ActiveObject("Microsoft.XMLHTTP“);

    }else if(window.XMLHTTPRequest) {  //非IE browser 存在XMLHTTPRequest js对象

           xhr = new window.XMLHTTPRequest();

    }

    return  xhr;

}

 

xhr = getXmlHttpRequest();    xhr.readyState=0; Uninitialized 初始化状态;

//设置头对象

xhr.open(method, url,async[,user, pass]); //method:get, post,head, async:true,false; 调用完readyState =1

xhr.setRequestHeader(name,value);  //可以设置表头,name不能含空白冒号换行,在open之后,send之前,如果已有header,会在后面追加。源内容, name=value

xhr.send(body);  当为post时,传递的参数体 ,当为get时, body=null,完成后状态为sent

xhr.onreadystatechange=function(){    //readystate状态变化触发

       if  (xhr.readyState ==3){  //表示receiving 正在接收状态

       }if(xhr.readyState ==4){  //表示loaded接收完成

               if (xhr.status ==200){ //表示成功返回  404没有找到 ; xhr.statusText 返回状态对应文本

                          document.getElementById("result").innerHtml=xhr.responseText //返回文本;同时还返回  xhr.responseXML

               }

       }

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值