jQuery 你能清除jquery ajax缓存吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2960810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you clear jquery ajax cache?
提问by chobo2
I am wondering if it is possible to clear the cache from a particular AJAX method.
我想知道是否可以从特定的 AJAX 方法中清除缓存。
Say I have this:
说我有这个:
$.ajax({
url: "test.html",
cache: true,
success: function(html){
$("#results").append(html);
}
});
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.
现在 99% 的情况下,可以使用缓存的结果,因为它应该始终具有相同的内容。但是,如果用户更新此内容,它(当然)会发生变化。如果它被缓存,它仍然会显示旧内容。
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.
所以,如果我可以为这个方法选择这个缓存并清除它并且所有其他缓存的东西都会保留,那就太酷了。
Can this be done?
这能做到吗?
Edit
编辑
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.
我不跟。我看到如果你设置cache
为 false,它会创建一个唯一的 URL 以防止浏览器缓存它。
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.
我的问题是我希望它被缓存,直到有人更新它。然后它不应该被缓存,直到他们再次点击它。然后它应该再次缓存。
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.
基本上,我有一个更新模型对话框(jquery UI),它会显示一个更新表单,以便用户可以更新表行。当他们单击“更新”时,它会更新该表格行。现在一列可以有几段数据,这会使表格看起来很糟糕。
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.
因此,为了保留表格,我在其中放置了一个名为“显示数据”的链接。现在,当单击它时,会出现一个对话框模型框,并从服务器中提取数据。
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.
如果他们点击它 5 次,它会重新加载 5 次。这就是为什么我想缓存它。但是,如果他们点击它并且它被缓存,那么无论出于何种原因他们去更新该行并单击“显示数据”,他们将获得缓存版本,而不是更新版本。
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).
我可能可以隐藏所有段落并使用 jquery 将它们显示出来,但我宁愿按需使用它。否则会隐藏很多废话,它会减慢页面速度(想象一下,如果某个人有 50 行,而这些列中的每一列都有 1000 个字符)。
回答by Oleg
You are misunderstanding the default cache: true
parameter of $.ajax
. In the documentation, you will find following:
您误解了 的默认cache: true
参数$.ajax
。在文档中,您会发现以下内容:
If set to false it will force the pages that you request to not be cached by the browser.
如果设置为 false,它将强制您请求的页面不被浏览器缓存。
To understand what this parameter really does, you should look at the jQuery source code:
要了解此参数的真正作用,您应该查看 jQuery 源代码:
if ( s.cache === false && type === "GET" ) {
var ts = now();
// try replacing _= if it is there
var ret = s.url.replace(rts, "_=" + ts + "");
// if nothing was replaced, add timestamp to the end
s.url = ret + ((ret === s.url) ?
(rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
}
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: false
,jQuery 只会向带有当前时间的 URL 添加一个附加参数。然后浏览器看到一个不同的 URL 并决定它的缓存中没有包含该 URL 的数据,因此它将请求转发到服务器。而已。
UPDATEDbased 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.
根据问题的已编辑部分更新:如果我理解正确,您想使用本地浏览器缓存,但您想控制它。如果是这样,您应该使用默认值 cache: true
(不要在 中添加此参数$.ajax
)。$.ajax()
您应该向服务器响应添加一些额外的缓存信息,而不是依赖于选项。浏览器将始终明确遵循缓存指令,因为它们被写入相应的页眉中。
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).
因此,例如,您可以向响应标头添加时间,指定页面的有效时间。如果您不需要客户端上数据的绝对最新版本(请参阅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
我在大多数应用程序中使用的另一种方法是将以下内容添加到服务器响应标头中
- "Cache-Control" set to "max-age=0", which switches off local caching
- "Etag" with a some value (for example, an MD5 hash of the data sent) to identify what the data contains. The value is absolutely free, you can calculate it any way you like, but two different responses should have different "Etag" values.
- “Cache-Control”设置为“max-age=0”,关闭本地缓存
- “Etag”带有一些值(例如,发送数据的 MD5 哈希值)以标识数据包含的内容。该值是完全免费的,您可以按您喜欢的任何方式计算它,但是两个不同的响应应该具有不同的“Etag”值。
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 emptyresponse 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.
如果您希望始终拥有最新版本的数据,但不希望服务器再次发送数据,则此方法非常适用于动态内容(例如,基于数据库中的数据的响应)自上次回复以来没有改变。如果您遵循此方法,则浏览器(每个浏览器)在第二次单击“显示数据”按钮时将发送到服务器的数据的标头添加来自"If-None-Match"
HTTP 请求标头内部分页的本地缓存的“Etag”值。然后服务器可以定义数据是否被更改。如果没有,服务器可以用空响应而"304 Not Modified"
不是"200 OK"
. 浏览器知道这一点,它直接从本地现金中获取数据。所以你的$.ajax
请求将成功结束,您将获得本地现金中的数据。
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)
您当然可以结合两种方式。只需设置一些非零值而不是“max-age = 0”,这是本地现金有效期的秒数(参见http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#第 14.9.3 节)