ASP/PHP/JSP等禁止ajax缓存的方法

来源:互联网 发布:数据融合 技术指标 编辑:程序博客网 时间:2024/05/17 01:43

ajax缓存有好处,但也有坏处,缓存有时候会导致误操作,影响用户体验,若你的WEB项目不需要ajax缓存功能,可按下述方法来禁止ajax缓存。

一、在ASP中禁止ajax缓存:

1'放在ASP网页最开头部分
2Response.expires=0
3Response.addHeader("pragma","no-cache")
4Response.addHeader("Cache-Control","no-cache, must-revalidate")

二、在PHP中禁止Ajax缓存:

1//放在PHP网页开头部分
2header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
3header("Cache-Control: no-cache, must-revalidate");
4header("Pragma: no-cache");

三、在JSp中禁止ajax缓存:

1//放在JSP网页最开头部分
2response.addHeader("Cache-Control""no-cache");
3response.addHeader("Expires""Thu, 01 Jan 1970 00:00:01 GMT");

四、通过给网页添加随机字符强制更新:如

1var url = 'http://url/';
2url += '?temp=' new Date().getTime();
3url += '?temp=' + Math.random();

五、若是静态HTML,可添加HTTP headers头禁止缓存,比如:

1<meta http-equiv="pragma" content="no-cache" />
2<meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />
3<meta http-equiv="expires" content="Thu, 01 Jan 1970 00:00:01 GMT" />
4<meta http-equiv="expires" content="0" />

六、可以在XMLHttpRequest发送请求之前加上以下代码禁止ajax缓存:

1XMLHttpRequest.setRequestHeader("If-Modified-Since","0");
2XMLHttpRequest.send(null);
0 0
原创粉丝点击