问题描述
浏览器自带后退按钮进行返回操作,浏览器返回操作默认读取缓存内容,而我们的需求是重新刷新页面,从服务器重新获取数据。一般使用了ajax进行异步刷新时会碰到这样的问题。
各种语境下解决方法
HTML解决方式
<pre>
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="0">
</pre>
js解决方式
(ps:可能有延时,因为页面会先解析其他内容)
window.onload = function(){
if(window.name!="hasLoad"){
location.reload();
window.name = "hasLoad";
}else{
window.name="";
}
}
Java解决方式
if (request.getProtocol().compareTo("HTTP/1.0") == 0){
response.setHeader("Pragma","no-cache");
}else if (request.getProtocol().compareTo("HTTP/1.1") == 0){
response.setHeader("Cache-Control","no-cache");
}
JSP解决方式
<%
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store");
%>
PHP解决方式
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
ASP解决方式