日期:2014-05-16  浏览次数:20616 次

ajax 异步调用
本帖最后由 showbo 于 2012-11-01 11:57:12 编辑
index.aspx:

<script language="javascript" type="text/javascript">
// <!CDATA[

var xmlhttp=false;
function createHTTP()
{
  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 getData() {

var sAnimal=document.getElementById("txt").value;

createHTTP();
xmlhttp.onreadystatechange=statechange;
xmlhttp.open("GET","DataPage.aspx?sAnimal="+sAnimal,true);
xmlhttp.send(null);

}
function statechange()
{
  if(xmlhttp.readystate==4)
  {
    if(xmlhttp.status==200)
    {
      FillData(xmlhttp.responsetext);
    }  
  }    
}
function FillData(sAnimal)
{  
   document.getElementById("DropDownList1").options.length=0;
   var index;
   var ani;
   while(sAnimal.length>0)
   {
      index=sAnimal.indexOf(",");
      if(index>0)
      {
      ani=sAnimal.substring)0,index);
      sAnimal=sAnimal.substring(index+1);
      document.getElementById("DropDownList1").add(new option(ani,ani));
      }
      else
      {
      lastAnimal=sAnimal.substring(0,2);
      document.getElementById("DropDownList1").add(new option(lastAnimal,lastAnimal));
      break;
      }
         
   }
}
// ]]>
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td colspan="3" style="height: 102px; text-align: center">
                    <strong><span style="font-size: 14pt">基于AJAX技术实现页面局部更新</span></strong></td>
      &nbs