表单重复提交2_禁止浏览器缓存

来源:互联网 发布:好看的搞笑网络短剧 编辑:程序博客网 时间:2024/05/21 12:35

禁止浏览器缓存-


如何禁止浏览器缓存,网上搜到的解决方法都测试无效。

基本上全都是

Cache-Control: no-cachePragma: no-cacheExpires: 0

Google了一下,找到了解决方法。

设置response header 的效果就是 返回的时候一定是重新请求页面的

Using PHP:

header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.header("Pragma: no-cache"); // HTTP 1.0.header("Expires: 0"); // Proxies.

Using Java Servlet, or Node.js:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.response.setHeader("Pragma", "no-cache"); // HTTP 1.0.response.setHeader("Expires", "0"); // Proxies.

Using ASP.NET:

Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.Response.AppendHeader("Expires", "0"); // Proxies.

Using ASP:

Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" ' HTTP 1.1.Response.addHeader "Pragma", "no-cache" ' HTTP 1.0.Response.addHeader "Expires", "0" ' Proxies.

Using Ruby on Rails, or Python on Flask:

response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.response.headers["Pragma"] = "no-cache" # HTTP 1.0.response.headers["Expires"] = "0" # Proxies.

Using Google Go:

responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.responseWriter.Header().Set("Pragma", "no-cache") // HTTP 1.0.responseWriter.Header().Set("Expires", "0") // Proxies.

Using Apache .htaccess file:

<IfModule mod_headers.c>    Header set Cache-Control "no-cache, no-store, must-revalidate"    Header set Pragma "no-cache"    Header set Expires 0</IfModule>

Using HTML4:

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

HTML meta tags vs HTTP response headers

根据SO上面的说法,

如果通过HTTP访问,HTTP response headers 是优先于 meta tags 的。但

是,第一次打开是通过HTTP访问的,而返回的时候是从本地读取的。


我在自己尝试的时候,发现这两个都需要设置 才能清除页面表单记录。Google浏览器 和 IE11测试通过,页面的记录消除。其他的浏览器未测试。(推测是因为上面的原因)
如果多次测试发现 页面表单的记录还在。

但是可以保证,只要写了HTTP response headers 返回的时候一定会重新请求。

部分代码如下
[html] view plain copy
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6. <%  
  7. response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.  
  8. response.setHeader("Pragma", "no-cache"); // HTTP 1.0.  
  9. response.setHeader("Expires", "0"); // Proxies.  
  10. %>  
  11.   
  12. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  13. <html>  
  14.   <head>  
  15.     <base href="<%=basePath%>">  
  16.     <title>index.jsp</title>  
  17.   
  18.     <meta http-equiv="pragma" content="no-cache">  
  19.     <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate" >  
  20.     <meta http-equiv="expires" content="0" >  
0 0