Ajax implementation of timed refresh, get back-end data (technology ASP)

Source: Internet
Author: User

Refer to XML dom,html Dom,javascript for example. When developing AJAX applications, you can always find what you need.
Now (), start our topic:

Here we use Access as a database, build a database name for User.mdb and update.html,update.asp the same directory, and then build the table

Update.html page, no explanation too much, there are comments

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >


<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 "/>
<title>untitled Document Network Major Camp Http://www.qqview.com</title>
<!--begin Stylesheet-->
<style type= "Text/css" >
Table.news_table ... {
border:1px;
border-style:dotted;
Border-color: #00CC99;
border-top:0;
font-size:14px;
Background: #FFFFFF;
Color: #669999;
}
Table.news_table thead TR ... {
Color: #FFFFFF;
Background: #0099CC;
border-bottom:2px solid #FF0000;
PADDING:2PX 10px;
}
Table.news_table tbody TR ... {
PADDING:2PX 10px;
}
</style>
<!--end Stylesheet-->
<script type= "Text/javascript" language= "JavaScript" >
var timeout = null; setinterval function handle
var xmlHttp = false; //
Initializing the XMLHttpRequest Object
function Createxmlhttp () ... {
XmlHttp = false;
if (window. ActiveXObject) ... {
Try ... {xmlHttp = new ActiveXObject ("Msxml2.xmlhttp");}
catch (E) ... {xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");}
}else if (window. XMLHttpRequest) ... {xmlHttp = new XMLHttpRequest ();}
}
Asynchronously invokes the Update.asp page to get the results timestamp in order not to let the browser cache the results
function SendRequest () ... {
Createxmlhttp ();
var url = "update.asp?timestamp=" + New Date (). GetTime ();
if (!xmlhttp) ... {
Alert ("XMLHttpRequest is not create!");
}
Xmlhttp.open ("GET", url, True);
Xmlhttp.onreadystatechange = function () ... {//callback function start
var tag = document.getElementById ("container");
tag.innerhtml = "";
if (xmlhttp.readystate = = 4 && xmlhttp.status = = 200) ... {
tag.innerhtml = Xmlhttp.responsetext;
}
}//callback function End
Xmlhttp.send (NULL);
}
Start Auto Refresh
function Update () ... {
Timeout = Window.setinterval ("SendRequest ()", 4000);//Set 4 seconds to invoke update.asp page
}
Stop automatic refresh
function Stopupdate () ... {
if (timeout! = null) ... {
Window.clearinterval (timeout);
}
}
</script>

<body onload= "SendRequest (); >
<input type= "button" value= "Start Fresh" onclick= "Update ();" />
<input type= "button" value= "Stop Fresh" onclick= "stopupdate ();" />
<div id= "container" ><!--container--></div>
</body>

Update.asp page, the server returns data, is called asynchronously by the Update.html XMLHttpRequest object, the code is as follows

<% @LANGUAGE = "VBSCRIPT" codepage= "936"%>
<%
Dim conn,rs,sql
Set Conn=server. CreateObject ("ADODB. Connection ")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server.mappath ("User.mdb")
Set Rs=server. CreateObject ("ADODB. RecordSet ")
Sql= "SELECT Top Ten * from News order BY ttime Desc"
Rs.Open sql,conn,1,3
%>
<table class= "news_table" cellpadding= "0" cellspacing= "0" >
<thead>
<tr>
<td>Id</td><td>Title</td><td>Ower</td><td>Time</td><td> Content</td>
</tr>
</thead>
<tbody>
<% '---Get the structure fill table---
If Rs.bof and rs.eof then
Response. Write ("no Record")
Else
While not rs.eof
Response. Write ("<tr>")
Response. Write ("<td>" &rs ("id") & "</td>")
Response. Write ("<td>" &rs ("title") & "</td>")
Response. Write ("<td>" &rs ("Ower") & "</td>")
Response. Write ("<td>" &rs ("Ttime") & "</td>")
Response. Write ("<td>" &rs ("Content") & "</td>")
Response. Write ("</tr>")
Rs.movenext
Wend
End If
'---Get the structure fill table-
%>
</tbody>
</table>
<%
' In order to see the updated result set every time this polygon is called, a record is deliberately added each time this page is called
Rs.addnew
RS ("title") =now () & "--title"
RS ("Ower") =request. QueryString ("timestamp")
RS ("content") = "--content" &now ()
Rs.update
' Release resources
Rs.close
Set rs=nothing
Conn.close
Set conn=nothing
%>

Open IE Test: (also successfully run under Firefox), notice the time column, we set the update frequency in the update.html is 4 seconds, the response to update.asp page query is also 4 seconds, proving that this test is no problem.

A simple timed Refresh page is done, but here we can put the updaet.html script into a class library for easy reuse.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.