XMLHTTP automatically update data in real time without refreshing

Source: Internet
Author: User
Tags sql return string split first row client
xml| Data | refresh | no refresh

Traditionally, we browsed the Web to add the latest data. It can only be displayed when we re requesting the server side. However, for some time-sensitive websites, the traditional approach is not enough.

We can get the program to automatically refresh and periodically request data from the server. 5 Seconds to take the data, 10 seconds to take the data. Use XMLHTTP to issue requests and obtain data. Uploaded to the client, the client organizes and displays the data.

Demo.htm foreground display.

 
  
<script language= "JavaScript" >function GetResult () {/**---------------getresult ()-----------------* GetResult () * Function: Send the request via XMLHTTP, return the result. * Parameter: str, string, send condition. * Instance: GetResult (); *---------------GetResult ()---------------- -*/var Obao = new ActiveXObject ("Microsoft.XMLHTTP");//special characters: +,%,&,=, etc transmission solution. The string is encoded first with escape.//update:2004-6-1 12:22obao.open ("POST", "server.asp", false); Obao.send ();//server-side processing returns an escape-encoded string. var strresult = unescape ( Obao.responsetext);//Separates strings. var Arrresult = strresult.split ("###"); Removerow (); Deletes the previous data.//separates the obtained strings and writes to the table. for (Var i=0;i<arrresult.length;i++) {arrtmp = Arrresult[i].split ("@@@"); NUM1 = ARRTMP[0]; The value of the field num1 num2 = arrtmp[1]; The value of the field num2 = Row1 = Tb.insertrow (); cell1 = Row1.insertcell (); cell1.innertext = Num1;cell2 = Row1.insertcell (); Cell2.innertext = num2;} function Removerow () {//retains the first row header and all remaining data is deleted. var irows = Tb.rows.length;for (var i=0;i<irows-1;i++) {Tb.deleterow (1);}} function Myshow () {//2 seconds automatically refresh once, 2 seconds to obtain data. Timer = Window.setinterval ("GetResult ()", 2000);} </script><body ><p></p><table width= "47%" height= "border=" "0" cellpadding= "1" cellspacing= "0" id= "TB" ><tr><td>num1</td><td>num2</td></tr></table >
 

server.asp Background Read Data

 
  
   
  <% @Language = "JavaScript"%><%function opendb (sdbname) {/**---------------opendb (sdbname)---------------- -* opendb (sdbname) * Function: Open database Sdbname, return conn object. * Parameters: Sdbname, String, database name. * instance: var conn = opendb ("Database.mdb"); *--------- ------OPENDB (sdbname)-----------------*/var connstr = "provider=microsoft.jet.oledb.4.0; Data source= "+server.mappath (Sdbname); var conn = Server.CreateObject (" ADODB. Connection "); Conn. Open (CONNSTR); return conn} var sresult = new Array (); var oconn = opendb ("Data.mdb");//special characters: +,%,&,=,? etc Transmission solutions. The client character is an escape encoded// So the server-side first has to go through unescape decoding.//update:2004-6-1 12:22var sql = "Select num1,num2 from Nums order by id"; var rs = oconn.execute (SQL) ; while (!rs. EOF) {//A record is separated by "###". Each column of data is separated by a "@@@". This is in the case of only two columns of data. Sresult[sresult.length] = rs ("Num1"). Value + "@@@" + RS ("num2"). Valuers.movenext ();} Escape solved the XMLHTTP. Chinese to deal with the problem. Response.Write (Escape (Sresult.join ("###"));%>
 
  

Database Data.mdb

Table Nums

ID, automatic numbering

NUM1, text

num2, text

Test data

ID NUM1 num2

1 20.70 20.810

2 10.5 20.5

3 12.3 300

4 132 323

5 563 56

6 20 10



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.