可以清除jquery ajax缓存吗? [英] Can you clear jquery ajax cache?

查看:126
本文介绍了可以清除jquery ajax缓存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能从特定的AJAX方法清除缓存.

I am wondering if it is possible to clear the cache from a particular AJAX method.

说我有这个:

$.ajax({
  url: "test.html",
  cache: true,
  success: function(html){
    $("#results").append(html);
  }
});

现在有99%的时间可以使用缓存的结果,因为它应该始终具有相同的内容.但是,如果用户更新了此内容,则它(当然)会更改.如果已缓存,它仍然会显示旧内容.

Now 99% of the time, a cached result can be used since it should always have the same content. However, if a user updates this content, it (of course) changes. If it is cached, it would still show the old content.

因此,如果我可以为此方法挑选该缓存并清除它,而所有其他缓存的东西都将保留,那就太酷了.

So, it would be cool if I could pick out this cache for this method and clear it and all the other cached stuff would stay.

可以做到吗?

修改

我不关注.我看到,如果将cache设置为false,它将创建一个唯一的URL,以防止浏览器对其进行缓存.

I do not follow. I see that if you set cache to false, it makes a unique URL to prevent the browser from caching it.

我的问题是我希望在有人对其进行更新之前将其缓存.然后,除非他们再次单击它,否则不应该对其进行缓存.然后应该再次将其缓存.

My problem is that I want it to be cached until someone does an update to it. Then it shouldn't be cached until they click on it again. Then it should be cached again.

基本上,我有一个更新模型对话框(jquery UI),它弹出一个更新表单,以便用户可以更新表行.当他们单击更新"时,它将更新该表行.现在,一列可以包含几个段落的数据,这会使表看起来很糟糕.

Basically, I have an update model dialog(jquery UI) that brings up an update form so that users can update a table row. When they click "Update," it updates that table row. Now one column can have, like, a couple of paragraphs worth of data and it makes the table look bad.

因此,要保留该表,我在其中放置了一个名为显示数据"的链接.现在,单击此按钮时,将显示一个对话框模型框,并从服务器中提取数据.

So to preserve the table, I have in it's place a link called "Show Data". Now, when this is clicked, a dialog model box shows up and the data is pulled from the server.

如果他们单击5次,它将重新加载5次.这就是为什么我要缓存它.但是,如果他们单击它并对其进行缓存,则无论出于何种原因,他们都去更新该行并单击显示数据",他们将获得缓存的版本,而不是更新的版本.

If they click on it 5 times it gets reloaded 5 times. That's why I want to cache it. However, if they click on it and it gets cached then for whatever reason they go and update that row and click on "Show Data," they will get the cached version, not the updated version.

我可能会隐藏所有段落,并使用jquery将它们显示在意愿上,但我宁愿按需使用它.否则,将会隐藏太多废话,这会减慢页面的速度(想象一下,如果某人有50行,而每列中的每一行都有1000个字符).

I could probably hide all the paragraphs and show them on will using jquery but I'd rather have it on demand. Otherwise there will be so much crap hidden and it will slow down the page (imagine if some guy has 50 rows and each one of those columns has like 1000 characters).

推荐答案

您误解了$.ajax的默认cache: true参数.在文档中,您将找到以下内容:

You are misunderstanding the default cache: true parameter of $.ajax. In the documentation, you will find following:

如果设置为false,将强制 您要求不要的页面 由浏览器缓存.

If set to false it will force the pages that you request to not be cached by the browser.

要了解此参数的实际作用,您应该查看jQuery源代码:

To understand what this parameter really does, you should look at the jQuery source code:

if ( s.cache === false && type === "GET" ) {
    var ts = now();

    // try replacing _= if it is there
    var ret = s.url.replace(rts, "$1_=" + ts + "$2");

    // if nothing was replaced, add timestamp to the end
    s.url = ret + ((ret === s.url) ?
            (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
}

因此,如果您使用cache: false,则jQuery只会在当前时间向URL添加一个附加参数.然后,浏览器将看到另一个URL,并确定该URL在其缓存中没有数据,因此将请求转发到服务器.没什么.

So if you use cache: false, jQuery just adds an additional parameter to the URL with the current time. The browser then sees a different URL and decides that it has no data in its cache with that URL, so it forwards the request to the server. Nothing more.

更新是基于问题的已编辑"部分:如果我对您的理解正确,则希望使用本地浏览器缓存,但希望对其进行控制.如果是这样,则应使用默认值cache: true(不要在$.ajax中添加此参数).不应依赖于$.ajax()选项,而应将一些其他缓存信息添加到服务器响应中.浏览器将始终明确遵循缓存指令,因为它们写在相应的页面标题中.

UPDATED based on the Edited part of the question: If I understand you correctly, you want to use the local browser cache, but you want control it. If so, you should use the default value of cache: true (don't add this parameter in the $.ajax). Instead of depending on the $.ajax() option, you should add some additional caching information to your server response. Browsers will always explicitly follow caching instructions as they are written in the corresponding page header.

例如,您可以在响应标头中添加一个时间,以指定页面有效的时间.如果您不需要客户端上的数据的绝对最新版本,这将非常有效(请参见

So, for example, you can either add a time to the response header specifying how long the page is valid. It is very effective if you don't need the absolute latest version of the data on the client (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html).

我在大多数应用程序中使用的另一种方法是将以下内容添加到服务器响应标头中

Another way, which I use in most of my applications, is to add the following to the server response headers

  1. "Cache-Control"设置为"max-age = 0",从而关闭了本地缓存
  2. 带有一些值的"Etag"(例如,所发送数据的MD5哈希值)以标识数据所包含的内容.该值是绝对免费的,您可以根据自己的喜好进行计算,但是两个不同的响应应具有不同的"Etag"值.

如果您希望始终拥有最新版本的数据,但又不想服务器发送数据,则此方法非常适合动态内容(例如,基于数据库数据的响应)如果自上次回复以来未更改过,请再次输入.如果您采用这种方法,则浏览器(每个浏览器)在第二次单击显示数据"按钮时,会将发送到服务器的数据的标题添加到来自"If-None-Match" HTTP请求中的本地兑现页面的"Etag"值标头.然后,服务器可以定义是否更改数据.如果不是,则服务器可以使用响应和"304 Not Modified"而不是"200 OK"进行响应.浏览器知道这一点,并直接从本地现金获取数据.因此,您的$.ajax请求将成功结束,并且您将从本地现金中获取数据.

This method is very good for dynamic contents (for example, for a response based on the data from the database) if you want to always have the latest version of the data, but don't want the server to send the data again if it hasn't changed since the last response. If you follow this method, the browser (every browser) add to the header of the data sending to the server at the second click on "Show Data" button the "Etag" value from the local cashed paged inside of "If-None-Match" HTTP request header. Then the server can define whether the data are changed. If not, server can response with an empty response and "304 Not Modified" instead of "200 OK". The browser knows this and it gets the data directly from the local cash. So your $.ajax request will be successful ended and you will have the data from the local cash.

您可以将两种方法结合起来.只需设置一些非零值,而不是"max-age = 0",这是本地现金有效期的时间(以秒为单位)(请参见

You can of cause combine two ways. Just set instead of "max-age=0" some non zero value which is the time in seconds of the validity of the local cash (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3)

这篇关于可以清除jquery ajax缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆