How to prevent the browser from caching a json file

后端 未结 4 1674
野趣味
野趣味 2020-12-04 17:09

So I\'m making this little project and I\'m having some troubles with catching. One thing that\'s not working is the browser keeps caching the json file that contains save d

相关标签:
4条回答
  • 2020-12-04 17:26

    if you dont want to cache any data then you can use the below meta tag

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="expires" content="-1">
    
    0 讨论(0)
  • 2020-12-04 17:32

    The easiest way is to append the source string with some random parameter, which gets ignored on the server side

    <script src="mySaveFiles.json?nocache=123" ></script>
    

    One solution would be to generate the script element using JavaScript and append the current time like this:

    var el = document.createElement( script );
    el.src = 'mySaveFiles.json?nocache=' + (new Date()).getTime();
    document.head.appendChild( el );
    

    That way, the browser will never cache the JSON-file as it appears to be a different file (due to the parameter) in every call.

    0 讨论(0)
  • 2020-12-04 17:37

    Two options:

    1. Add a mySaveFiles.json?t=timestamp query parameter to the end of the url.
    2. Pull the file in with the XmlHttpRequest object (you still may need to add a timestamp depending on the server).
    0 讨论(0)
  • 2020-12-04 17:41

    One of my favorites is just htaccess (if this is possible for you, I can't see that)

    Disable cache for multiple extensions

    <FilesMatch ".(pl|php|cgi|spl|scgi|fcgi|json)$">
        Header unset Cache-Control
    </FilesMatch>
    

    Disable cache for just 1 extension

    <Files .json>
        Header unset Cache-Control
    </Files>
    

    I found it here: http://www.queness.com/post/5421/17-useful-htaccess-tricks-and-tips

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题