简体   繁体   English

jQuery如何禁用AJAX缓存?

[英]How does jQuery disable AJAX caching?

To disable caching files from ajax requests, you can use jQuery's 要禁用来自ajax请求的缓存文件,您可以使用jQuery的

$.ajaxSetup({
    cache: false
});

But how does jQuery do this? 但是jQuery如何做到这一点? I know jQuery is a javascript library, so whatever can be done with jQuery can be done with plain javascript. 我知道jQuery是一个javascript库,因此使用jQuery可以完成的任何操作都可以使用普通javascript来完成。 So my question is: What is the javascript code that jQuery uses under the hood to turn off ajax file caching ? 所以我的问题是: jQuery用来关闭ajax文件缓存的javascript代码什么

I think that Today's Browsers use onunload = function(){} just like that (yes, exactly) to prevent the Browser from caching a web page, as it was when you left it to go to another page. 我认为今天的浏览器使用onunload = function(){}就像那样确实是)来防止浏览器缓存网页,就像您离开该网页进入另一个页面时一样。

It's important to under stand, however, that that is not the same as the Browser's ability to remember the JavaScript loaded from your <script type='text/javascript' src='somePage.js'></script> tags when they have that src attribute. 但是,重要的是要明白,这与浏览器记住从<script type='text/javascript' src='somePage.js'></script>标记加载的JavaScript的能力不同该src属性。 If you change your JavaScript on a live site, you'll want to change the name of that file, or, if the Client has not cleared their cache their Browser will attempt to load the file as it remembers it. 如果您在实时站点上更改JavaScript,则需要更改该文件的名称,或者,如果客户端尚未清除其缓存,则其浏览器将尝试记住该文件来加载该文件。

This is the source of the cache 这是缓存的来源

        if ( s.cache === false ) {
            s.url = rts.test( cacheURL ) ?

                // If there is already a '_' parameter, set its value
                cacheURL.replace( rts, "$1_=" + nonce++ ) :

                // Otherwise add one to the end
                cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++;
        }

s is ajax's option, If you set cache false, It will add a search to you request url, The 'nonce' is jQuery.now(), It's a time; s是ajax的选项,如果将cache设置为false,它将向您的请求url添加搜索,“ nonce”是jQuery.now(),这是一个时间; So browser will not user cache when you send ajax , request url always differenrt. 因此,当您发送ajax时,浏览器将不会缓存用户,请求url始终是不相同的。

The easiest way to shut off browser caching of Ajax requests is with a query string parameter based on time. 关闭浏览器对Ajax请求的缓存的最简单方法是使用基于时间的查询字符串参数。

var t = new Date().getTime();

console.log('some-url?_='+t);

This yields the following query string 这将产生以下查询字符串

?_=1481683928873 ?_ = 1481683928873

The browser will see this as a different request (assuming it only makes one per microsecond) and it will request the content from the server, rather then serving it from its cache. 浏览器会将此视为不同的请求(假设它每微秒仅发出一个请求),它将从服务器请求内容,而不是从其缓存中提供内容。

If you read the docs they say: 如果您阅读文档,他们会说:

cache (default: true, false for dataType 'script' and 'jsonp')
Type: Boolean

If set to false, it will force requested pages not to be cached by the browser. 如果设置为false,将强制浏览器不缓存请求的页面。 Note: Setting cache to false will only work correctly with HEAD and GET requests. 注意:将缓存设置为false只能与HEAD和GET请求一起正常使用。 It works by appending "_={timestamp}" to the GET parameters. 它通过在GET参数后附加“ _ = {timestamp}”来工作。 The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET. 对于其他类型的请求,不需要此参数,但在IE8中,当对GET已经请求的URL进行POST时,则不需要该参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM