ajax无刷新更新数据
ajax.html
<!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> </head> <script> var xhr; function CreateXmlHttp() { if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } else { xhr = new XMLHttpRequest(); } } function SendSeq() { CreateXmlHttp(); xhr.open("GET", "Handler.ashx", true); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { if (xhr.responseText != "") { document.getElementById("Text1").value = xhr.responseText; } } } xhr.send(null); } window.onload = function () { var id = setInterval("SendSeq()", 1000); } </script> <body> <form id="form1" runat="server"> <div> <input id="Text1" type="text" /> </div> </form> </body> </html>
Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; context.Response.Write("10"); context.Response.Cache.SetNoStore();//设置无缓存 } public bool IsReusable { get { return false; } } }