您的位置:首页 > 其它

关于ajax在IE下缓存的问题

2013-03-22 15:00 337 查看
采用AJAX技术的时候 通常我们无刷新页面提交数据后 用同样的url去获取数据的时候会发现是以前的数据~那样就给client端带来假象了~~ 采用以下的方法可以取消缓存 

htm网页 

<metahttp-equiv="pragma"content="no-cache"> 

<metahttp-equiv="cache-control"content="no-cache,must-revalidate"> 

<metahttp-equiv="expires"content="wed,26feb199708:21:57gmt"> 

或者<metahttp-equiv="expires"content="0"> 

asp网页 

response.expires=-1 

response.expiresabsolute=now()-1 

response.cachecontrol="no-cache" 

php网页 

header ("Cache-Control: pre-check=0, post-check=0, max-age=0", false);

header("expires:mon,26jul199705:00:00gmt"); 

header("cache-control:no-cache,must-revalidate"); 

header("pragma:no-cache");  

jsp网页

response.addHeader("pragma", "no-cache");

response.addHeader("cache-control", "no-cache,must-revalidate");

response.addHeader("expires", "0");

asp.net

只要在aspx.cs代码的Page_Load事件中加上

Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

即可.

IE下的缓存问题

由于IE的缓存处理机制问题,每次通过XMLHttpRequest访问动态页面返回的总是首次访问的内容,解决方法有:

1、客户端通过添加随机字符串解决。如:

var url = 'http://dancewithnet.com/';

url +=  '?temp=' + new Date().getTime();

url +=  '?temp=' + Math.random();

2、在HTTP headers禁止缓存。如:

HTTP:

<meta http-equiv="pragma" content="no-cache" />

<meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />

<meta http-equiv="expires" content="Thu, 01 Jan 1970 00:00:01 GMT" />

<meta http-equiv="expires" content="0" />

PHP:

header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");

header("Cache-Control: no-cache, must-revalidate");

header("Pragma: no-cache");

ASP:

Response.expires=0

Response.addHeader("pragma","no-cache")

Response.addHeader("Cache-Control","no-cache, must-revalidate")

JSP:

response.addHeader("Cache-Control", "no-cache");

response.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT");

3、在XMLHttpRequest发送请求之前加上:

XMLHttpRequest.setRequestHeader("If-Modified-Since","0");

或者XMLHttpRequest.setRequestHeader("Cache-Control","no-cache");

然后再

XMLHttpRequest.send(null);

注:XMLHttpRequest可以是你定义的对象,例如是:xmlhtp = new XMLHttpRequest();那上面的XMLHttpRequest就是xmlhttp。

4、IE下的reponseXML问题

使用responseXML时,IE下只能接受.xml为后缀的XML文件,如果不能以.xml文件为结尾的,则需要如下处理:

在服务器端声明是xml文件类型。如:

PHP:header("Content-Type:text/xml;charset=utf-8");

ASP:Response.ContentType = "text/xml";

JSP:response.setHeader("ContentType","text/xml");

利用responseText获取,然后封装成XML。

在AJAX应用上,JSON和JsonML是XML非常好的替代品。

5、用POST替代GET:不推荐

 

在Firefox中测试了一下,不存在这个问题。

另外,在Firefox中脚本是严格区分大小写的,IE并不严格区分。

例如:xmlHttpRequest.readyState其中“State”的“S”必须大写,否则在IE中正常的功能在Firefox中无法实现,会出现undefined错误。

另:PHP后退页面过期或不存在问题的解决

post后或使用了使用了session都有可能引起这种情况

解决办法,在开头加入如下代码即可:

header("Cache-control: private");

本文转载自『北漂石头的博客』
http://www.niutian365.com/blog/

更多精彩内容,欢迎访问北漂石头的博客!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: