Ajax异步通信关键代码(示例)

以下是js代码
var xmlHttp = false;
try {
  xmlHttp 
= new ActiveXObject("Msxml2.XMLHTTP");
catch (e) {
  
try {
    xmlHttp 
= new ActiveXObject("Microsoft.XMLHTTP");
  } 
catch (e2) {
    xmlHttp 
= false;
  }
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){
  xmlHttp 
= new XMLHttpRequest();
}
function callBack()
{
  
var url="test.asp";//提交给后台执行页面
  xmlHttp.open("POST",url,true);
  xmlHttp.onreadystatechange
=updatePage;
  xmlHttp.setRequestHeader(
"Content-Type","application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader(
"content","text/html");
  xmlHttp.setRequestHeader(
"charset","utf-8");
  xmlHttp.send(
"flag=yes");
  
}
function updatePage()
{
    
if(xmlHttp.readyState<4)//test.asp未执行完显示的内容
    {
        getInfo.innerHTML
="正在获取数据";
    }
    
if(xmlHttp.readyState==4)//执行完以后显示返回的信息!
    {
        getInfo.innerHTML
=xmlHttp.responseText;
    }
}
页面代码:
<div id="getInfo">
</div>
<a onClick="callBack()">click</a>
posted @ 2009-04-09 17:33  小胆粗心  Views(373)  Comments(0)    收藏  举报