要完整实现一个AJAX异步调用和局部刷新,一般须要如下几个步骤:javascript
(1)建立XMLHttpRequest对象,也就是建立一个异步调用对象. //IE6以上 var xhr= new XMLHttpRequest(); //IE6 var xhr=new ActiveXObject("Microsoft.XMLHTTP") (2)建立一个新的HTTP请求,并指定该HTTP请求的方法、URL及验证信息. xhr.open(方法,url,是否异步) (3)设置响应HTTP请求状态变化的函数. <script type="text/javascript"> function getDoc(){ var xmlhttp; if(window.xmlhttpRequest){ xmlhttp=new XMLHttpRequest(); } else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//for IE6 } xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ document.getElementById("myId").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET", index.php,true); xmlhttp.send(); } </script> </head> <body> <button type="button" onclick="getDoc()">请求数据</button> </body> (4)发送HTTP请求. xmlhttp.send(); (5)获取异步调用返回的数据. (6)使用JavaScript和DOM实现局部刷新.