jhlovett 发表于 2013-1-23 02:28:11

Ajax避免IE缓存造成每次不向服务器提交数据

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %><!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>    <script type="text/javascript">      var xmlHttp;      function createXMLHttpRequest() {            try {                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");//IE6            } catch (e) {                try {                  xmlHttp = new ActiveXObject("Micorsoft.XMLHTTP");                } catch (e) {                  xmlHttp = new XMLHttpRequest();                }            }      }      function startRequest() {            createXMLHttpRequest();            xmlHttp.open("get", convertURL("Default4.aspx?id=test"), true);            xmlHttp.onreadystatechange = function() {                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {                  document.getElementById("msg").innerHTML = xmlHttp.responseText;                }            }            xmlHttp.send(null);      }      /*利用"时间戳"的伪地址骗过浏览器.          在发送url请求时加上一个参数,这个参数是时间戳,就是系统时间.      (以毫秒为单位的数字类型).      */      function convertURL(url) {            var timestamp = new Date().valueOf();            if (url.indexOf("?") >= 0) {                url +="&+=" + timestamp;            } else {                url += "?+=" + timestamp;            }            return url;      }    </script></head><body>    <form id="form1" runat="server">    <input type="button"value="测试" />    <div id="msg">    </div>    </form></body></html> 
 
protected void Page_Load(object sender, EventArgs e)    {      if (!String.IsNullOrEmpty(Request.QueryString["id"]))      {            Response.Write(DateTime.Now);            Response.Write("<br>");            Response.Write(Request.Url);            Response.Write("<br>");            Response.Write(Request.UrlReferrer);            /*这里设置浏览器缓存页面过期时间,这里设置为0分钟则代表浏览器不缓存,每次提交都会和服务器交互.            *浏览器缓存的概念:当客户发出第一次请求的时候,浏览器会把这次提交的数据放在缓存中,当该用户再次访问            *该页面的时候,浏览器会去查找该页面缓存是否有改动,有则再次与服务器交互,无则调用浏览器缓存.*/            // Response.Expires = 0;                /*因为请求的是当前页面,除了返回上面输出的信息外还会返回整个HTML文档,这样就不是局部    *刷新了(也就是服务器回传postback后会将整个页面再次输出),所以我们输出以上信息后,让服务器停止对客户端响应,这样客户端只会得到一小部分    *你想要更新的数据,这就是局部刷新.(当然做法还有放到HttpHandle或WebService中进行操作)            */    Response.End();      }      else      {            if (IsPostBack)            {                Response.Clear();                Response.End();            }      }    }  
HELP:
JavaScript valueOf() 方法

<div class="backtoreference">JavaScript Date 对象参考手册
页: [1]
查看完整版本: Ajax避免IE缓存造成每次不向服务器提交数据