要完全完成一个AJAX异步挪用和部分革新,一般须要以下几个步骤:
(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完成部分革新.