XMLHttpRequst对象的使用

来源:互联网 发布:windows10平板优化版 编辑:程序博客网 时间:2024/05/01 17:51

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<script type="text/javascript" language="javascript">
 var xmlHttp;
function createXMLHttpRequest()
{
  //在IE下创建XMLHttpRequest对象
  try
  {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e)
  {      
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  catch(oc)
  {
    xmlHttp = null;
  }
 }
  //在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象
  if(!xmlHttp && typeof XMLHttpRequest != "undefined")
 {
  xmlHttp = new XMLHttpRequest();
 }
return xmlHttp;
}
function send_request(url)  
{
  createXMLHttpRequest();
  xmlHttp.onreadystatechange  = processRequest;  
  xmlHttp.open("GET",   url,   true);  
  xmlHttp.send(null);  
}  
//   处理返回信息的函数  
function   processRequest()    
  {  
      if   (xmlHttp.readyState   ==   4)    
      {   //   判断对象状态  
          if   (xmlHttp.status   ==   200)  
          {   //   信息已经成功返回,开始处理信息 
          
          document.getElementById(currentPos).innerHTML = xmlHttp.responseText;  
          }    
          else    
          {   //页面不正常  
          alert("您所请求的页面有异常。");  
          }  
      }  
  }  
   
  function   showRoles(obj,obj2)    
  {  
    currentPos   =   obj;  
  document.getElementById(obj).parentNode.style.display   =   "";  
  document.getElementById(obj).innerHTML   =   "正在读取数据..."  
 
  send_request("Server.aspx?phonenum="+document.getElementById(obj2).value+"&r" + Math.random());  
  }  

</script>
<body>
<form action="#">
 <select name="number" id="number">  
  <option  value="888888">888888</option>  
  <option  selected="selected"   value="777777">777777</option>  
  </select>
  <a   href="#" onclick="showRoles('test','number')">立即查询</a>
  <table>
  <tr style="display:none   ">  
  <td id="test" height="20">&nbsp;</td>  
  </tr>  
  </table>
</form>
</body>
</html>
Server.aspx页面

 

<%@ Page Language="C#"%>
<script runat="server" type="text/C#">
    protected void Page_Load(object sender,EventArgs e)
    {
        string phonenum =Convert.ToString(Request["phonenum"]);
        Response.Write(phonenum + "|" + DateTime.Now.ToString());
    }
</script>
这是我看别人的例子修改的,运行出来了,可以局部刷新页面。修改显示的时间。
原创粉丝点击