The example in this article describes how JQuery implements a simple server polling effect. Share it with everyone for your reference, the details are as follows:
After entering many forums, a prompt pops up saying how many emails have not been read, or an oa system prompts how many tasks have not been completed after entering. It will prompt once every once in a while, but how to implement it. In fact, it will be relatively simple to use jquery. The core elements are json format parsing and setInterval() function. Let’s implement it together:
First, our default.aspx page looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 runat="server"> <title>服务器轮询</title> <link href="Content/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" /> <link href="Content/ui.jqgrid.css" rel="stylesheet" type="text/css" /> <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="js/i18n/grid.locale-en.js" type="text/javascript"></script> <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script> <script src="js/src/grid.base.js" type="text/javascript"></script> <script type="text/javascript"> function showUnreadNews() { $(document).ready(function() { $.ajax({ type: "GET", url: "Result.ashx", dataType: "json", success: function(msg) { //alert(msg); $.each(msg, function(id, title) { $("#news").append("<a href=detailnews.aspx?id=" + id + ">" + title + "</a><br>"); }); } }); }); } setInterval('showUnreadNews()',5000); </script> </head> <body> <form id="form1" runat="server"> <div id="news"> </div> </form> </body> </html>
The above code mainly uses the ajax function to issue an ajax request to the Result.ashx page, and then the Result.ashx page returns the json data and parses it. Finally, the setInterval() function is used to achieve the polling effect. The specific Result.ashx page The code is as follows:
<%@ WebHandler Language="C#" Class="Result" %> using System; using System.Web; using System.Text; using System.Data.SQLite; using System.Data; public class Result : IHttpHandler { public void ProcessRequest (HttpContext context) { string sql = "select * from Content where NewsFlag=0"; DataTable dt = new DataTable(); using (SQLiteConnection conn = new SQLiteConnection(InitSQLite().Connection)) { SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, conn); sda.Fill(dt); } string jsonStr = GetJson(dt); context.Response.ContentType = "text/json"; context.Response.Buffer = true; context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1); context.Response.AddHeader("pragma", "no-cache"); context.Response.AddHeader("cache-control", ""); context.Response.CacheControl = "no-cache"; context.Response.Write(jsonStr); } public static string GetJson(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{"); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append( dt.Rows[i]["NewsID"].ToString() + ":" +"\""+ dt.Rows[i]["NewsTitle"].ToString()+"\","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("}"); return jsonBuilder.ToString(); } public Sqlite InitSQLite() { Sqlite _sqLite = new Sqlite(); _sqLite.ConnetStringBuilder.DataSource = AppDomain.CurrentDomain.BaseDirectory + "News.db3"; _sqLite.ConnetStringBuilder.Pooling = true; _sqLite.ConnetStringBuilder.FailIfMissing = true; _sqLite.ConnetStringBuilder.UseUTF16Encoding = true; return _sqLite; } public bool IsReusable { get { return false; } } }
The database uses sqlite, please check yourself for the specific usage. In this processing file, the most important thing is to generate json data that conforms to the format from the datatable data.
In this way, the system is finally implemented. Every 5 seconds, the homepage will poll the server for data in order to obtain the latest data.
Readers who are interested in more jQuery related content can check out the special topics on this site: "JQuery switching effects and techniques summary", "jQuery drag effects and techniques summary", "JQuery extension skills summary", "jQuery common classic special effects summary", "jQuery animation and special effects usage summary", "jquery selector usage Summary " and "Summary of jQuery common plug-ins and usage "
I hope this article will be helpful to everyone in jQuery programming.