Ajax long polling and ajax polling
Front-end code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Wait. aspx. cs" Inherits = "Web Listener. Wait" %> <! 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 ="/JS/easyui/easyui.css "rel =" stylesheet "type =" text/css "/> <script src ="/JS/jquery. min. js "type =" text/javascript "> </script> <script src ="/JS/easyui/jquery. easyui. min. js "type =" text/javascript "> </script> <script src ="/JS/SimpoWindow. js "type =" text/javascript "> </script> <script type =" text/javascript "> var longPollingCount; $ (function () {longPollin GCount = 1; $ ("# msg" ).html ("" + longPollingCount + "times request ...... "); longPolling () ;}); function longPolling () {$. ajax ({type: "POST", url: "Check. aspx? Action = check ", dataType:" text ", timeout: 5000, error: function (XMLHttpRequest, textStatus, errorThrown) {longPollingCount ++; $ ("# msg" ).html ("" + longPollingCount + "Requests ...... "); if (textStatus =" timeout ") {// request timeout longPolling (); // recursive call} else {// other errors, such as network errors $ ("# msg" ).html (textStatus); longPolling () ;}}, success: function (data, textStatus) {var parent = SimpoWin. getWinParent (); $ ("# txt" ).html ("Operation completed ...... "); $ (" # txt ").css (" color "," red "); setTimeout (function () {SimpoWin. closeWin (); parent. next (data) ;}, 1000) ;}}</script> Background code:
Using System; using System. threading; namespace Web Listener {public partial class Check: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {string action = Request ["action"]; switch (action) {case "check": while (true) {if (Common. flag <160) {Common. flag ++; Thread. sleep (100);} else {Response. write ("return information after the operation is completed"); Response. end (); break ;}} break ;}}}}View Code