用AJAX实现无刷新数据读取与显示

来源:互联网 发布:淘宝卖果茶需要什么 编辑:程序博客网 时间:2024/04/19 13:21

*******数据显示页ajax.asp:

<script type="text/javascript">
<!--
/*
 * Author: Wintalen
 * Email: winalen (at) 163.com
 * OICQ: 83151085
 * CopyRight: www.jgzx.net
*/
//3个用于实现无刷新数据读取的函数
var xmlHttp ;
function createXMLHttpRequest()
{
 if(window.ActiveXObject)
 {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 }
 else if(window.XMLHttpRequest) {
  xmlHttp = new XMLHttpRequest();
 }
}

function startRequest()
{
 createXMLHttpRequest();
 var url = "test.asp";
 xmlHttp.onreadystatechange = handleStateChange;
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
 setTimeout("startRequest()",1000);
}

function handleStateChange()
{
 if(xmlHttp.readyState == 1)
 {
  document.getElementById("weather").innerHTML = "正在连接服务器......";
 }
 else if(xmlHttp.readyState == 2)
 {
  document.getElementById("weather").innerHTML = "正在加载信息......";
 }
 else if(xmlHttp.readyState == 4)
 {
  if(xmlHttp.status == 200)
  {
   document.getElementById("show").innerHTML = xmlHttp.responseText;
  }
  else
  {
   document.getElementById("weather").innerHTML = "数据读取失败,请稍后再试......";
  }
 } 
 else
 {
  document.getElementById("weather").innerHTML = "服务器连接失败!"; 
 }
}
//-->
</script>
<body onload="startRequest();">
<div id="show"></div>
</body> 

 

********数据生成页test.asp:

<%
'下面这两行不能少,一定要加上才能实现更新效果
Response.Buffer=true
Response.Expires=0
%>
<%=now()%>

原创粉丝点击