一.局部刷新

 1.创建异步对象:

var xmlHttp =new XMLHttpRequest(); 

 2.绑定事件:

xmlHttp.onreadystatechange =function () {
    //处理服务端返回的数据,更新当前页面
    // alert("readyState属性值: "+xmlHttp.readyState)
    if (xmlHttp.readyState == 4 && xmlHttp.status ==200){
        //alert(xmlHttp.responseText);
        //更新dom对象
        var data =xmlHttp.responseText;
        document.getElementById("mydata").innerText =data;
    }
}

3.初始请求对象

//获取dom对象的value值
var name =document.getElementById("name").value;
var w = document.getElementById("w").value;
var h = document.getElementById("h").value;
//传参数,拼接字符串
var param="name="+name +"&w="+w+"&h="+h;
//alert("param="+param);
//true使用异步处理对象请求后,不需要等待数据处理完毕就可进行其他操作;
//false同步,异步对象必须完成请求从服务端获取数据后,才能send之后的代码,任意时刻只能执行一个异步请求。 xmlHttp.open("get","bmiAjax?"+param,true)//get是访问方式,bmiAjax是访问地址

4.发送请求

xmlHttp.send();

**Ajax工作原理 参考:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/18/2216553.html

posted on 2021-09-27 22:57  比企苦  阅读(13)  评论(0编辑  收藏  举报