美文网首页
如何实现页面每次打开时清除本页缓存

如何实现页面每次打开时清除本页缓存

作者: bestCindy | 来源:发表于2020-10-14 20:23 被阅读0次

(一)可以用 HTML 标签设置 http 头部信息

<header>
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" contant="no-cache">
</header>
  • Cache-Control
    • no-cache:强制从服务器上获取新的页面
    • no-store:在任何环境下不缓存任何页面
  • Pragma
    • http1.0 规范中的 Pragma: no-cache 等同于 http1.1 规范中的 Cache-Control: no-cache

(二)在需要打开的 url 后面增加一个随机参数

  • 增加参数之前:url = test/test.jsp
  • 增加参数之后:url= test/test.jsp?ranparam=random();

说明:因为每次请求的 url 后面的参数都不一样,相当于重新请求页面

(三)设置 chrome

chrome 在 developer tools 的 settings 中有一个 Disable cache 选项

(四)在 ajax 中设置

设置 http 请求头

$.ajax({
     url: 'www.xxxxx.com',
     dataType: 'json',
     data: {},
     beforeSend :function(xmlHttp){ 
        xmlHttp.setRequestHeader("If-Modified-Since","0"); 
        xmlHttp.setRequestHeader("Cache-Control","no-cache");
     },
     success: function(response) { }
});

或者,设置 cache

$.ajax({
     url: 'www.haorooms.com',
     dataType: 'json',
     data: {},
     cache: false, 
      success:function(response) {
});

相关文章

网友评论

      本文标题:如何实现页面每次打开时清除本页缓存

      本文链接:https://www.haomeiwen.com/subject/fkzwpktx.html