ajax学习

2018-07-08  本文已影响0人  秋玄语道
var xmlHttpReq = null;  //声明一个空对象用来装入XMLHttpRequest
    if (window.ActiveXObject){//IE5 IE6是以ActiveXObject的方式引入XMLHttpRequest的
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest){//除IE5 IE6 以外的浏览器XMLHttpRequest是window的子对象
        xmlHttpReq = new XMLHttpRequest();//实例化一个XMLHttpRequest
    }
    if(xmlHttpReq != null){ //如果对象实例化成功 
        xmlHttpReq.open("GET","localhost/test.php",true);   //调用open()方法并采用异步方式
        xmlHttpReq.onreadystatechange=RequestCallBack; //设置回调函数
        xmlHttpReq.send(null);  //因为使用get方式提交,所以可以使用null参调用
    }
    function RequestCallBack(){//一旦readyState值改变,将会调用这个函数
        if(xmlHttpReq.readyState == 4){
                if(xmlHttpReq.status == 200){
                    //将xmlHttpReq.responseText的值赋给ID为 resText 的元素
                    document.getElementById("resText").innerHTML = xmlHttpReq.responseText;
                }
        }
    }
image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读