每隔 指定 时间取数据
-----------------------------
XmlHttp.Htm
-----------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>XmlHttp无刷新测试</title>
<script language="javascript">
<!--
function XmlHttp()
{
document.all.MyDiv.innerHTML = "数据加载中..."
//*****
var http = new ActiveXObject("Microsoft.XMLHTTP");
http.open("POST","XmlHttp.Asp",false);
http.send();
//var http= new ActiveXObject("MSXML2.XMLHTTP.4.0");
//http.open("GET","XmlHttp.Asp",false,"","");
//http.send("");
var str = unescape(http.responseText); //unescape,对应escape使用
//*****
document.all.MyDiv.innerHTML = str;
}
setInterval("XmlHttp();",1000); //每秒钟刷新一次
//-->
</script>
</head>
<body onLoad="XmlHttp();">
<div id="aaa"></div>
<div id="MyDiv"></div>
</body>
</html>
-----------------------------------
XmlHttp.Asp
-----------------------------------
<% @Language="JavaScript" %>
<%
//Asp,JavaScript 脚本的
var SendContent="";
var ConnStr = "Driver={Sql Server};Server=localhost;DataBase=AspNet;Uid=sa;Pwd=as;";
var Conn = Server.CreateObject("Adodb.Connection");
Conn.ConnectionString = ConnStr;
Conn.Open();
var Sql="Select * from News Order by id Desc";
var rs=Conn.Execute(Sql);
SendContent = "<table border=1 style=border-collapse:collapse align=center>";
SendContent += "<tr>";
SendContent += "<td align=center>新闻标题</td>";
SendContent += "<td align=center>发布时间</td>";
SendContent += "</tr>";
while(!rs.eof)
{
SendContent += "<tr>";
SendContent += "<td align=center>"+ rs("NewsTitle") +"</td>";
SendContent += "<td align=center>"+ rs("NewsDate") +"</td>";
SendContent += "</tr>";
rs.MoveNext;
}
SendContent += "</table><br>";
var d = new Date();
SendContent += "当前时间: " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
Response.Write(escape(SendContent)); //escape解决中文乱码问题
rs.Close();
Conn.Close();
%>
****************************************
function GetResult()
{
var DataToSend = "这里是发送数据";
var http = new ActiveXObject("Microsoft.XMLHTTP");
http.open("POST","News.aspx",false);
http.setRequestHeader("ContentType", "application/xwwwformurlencoded");
http.send(DataToSend);
result = http.responseText;
return result;
}
News.aspx
System.IO.StreamReader red = new StreamReader(Request.InputStream,System.Text.Encoding.UTF8/*有可能是gb2312*/);
string strContent = red.ReadToEnd();