看起来,如果我使用$.get()加载动态内容,结果会缓存在浏览器中。

在QueryString中添加一些随机字符串似乎解决了这个问题(我使用new Date(). tostring()),但这感觉像是一个黑客。

还有别的办法吗? 或者,如果唯一字符串是实现这一点的唯一方法,除了new Date()还有其他建议吗?


当前回答

The real question is why you need this to not be cached. If it should not be cached because it changes all the time, the server should specify to not cache the resource. If it just changes sometimes (because one of the resources it depends on can change), and if the client code has a way of knowing about it, it can append a dummy parameter to the url that is computed from some hash or last modified date of those resources (that's what we do in Microsoft Ajax script resources so they can be cached forever but new versions can still be served as they appear). If the client can't know of changes, the correct way should be for the server to handle HEAD requests properly and tell the client whether to use the cached version or not. Seems to me like appending a random parameter or telling from the client to never cache is wrong because cacheability is a property of the server resource, and so should be decided server-side. Another question to ask oneself is should this resource really be served through GET or should it go through POST? That is a question of semantics, but it also has security implications (there are attacks that work only if the server allows for GET). POST will not get cached.

其他回答

也许你应该看看$.ajax()(如果你使用的是jQuery,它看起来就像jQuery)。 看一下:http://docs.jquery.com/Ajax/jQuery.ajax#options和选项“缓存”。

另一种方法是研究如何在服务器端缓存内容。

The real question is why you need this to not be cached. If it should not be cached because it changes all the time, the server should specify to not cache the resource. If it just changes sometimes (because one of the resources it depends on can change), and if the client code has a way of knowing about it, it can append a dummy parameter to the url that is computed from some hash or last modified date of those resources (that's what we do in Microsoft Ajax script resources so they can be cached forever but new versions can still be served as they appear). If the client can't know of changes, the correct way should be for the server to handle HEAD requests properly and tell the client whether to use the cached version or not. Seems to me like appending a random parameter or telling from the client to never cache is wrong because cacheability is a property of the server resource, and so should be decided server-side. Another question to ask oneself is should this resource really be served through GET or should it go through POST? That is a question of semantics, but it also has security implications (there are attacks that work only if the server allows for GET). POST will not get cached.

如果你正在使用。net ASP MVC,通过在end point函数上添加以下属性禁用控制器动作上的缓存:

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]

Internet Explorer的Ajax缓存:你打算怎么做?提出了三种方法:

在查询字符串中添加一个缓存破坏令牌,比如?date=[timestamp]。在jQuery和YUI中,您可以告诉它们自动执行此操作。 使用POST而不是GET 发送一个HTTP响应头,明确禁止浏览器缓存它

这里所有的答案都在请求的URL上留下一个足迹,这将显示在服务器的访问日志中。

我需要一个基于头的解决方案,没有副作用,我发现它可以通过设置在如何控制网页缓存中提到的头来实现,在所有浏览器?

结果是,至少在Chrome上是这样的:

. ajax({美元 url: url、 标题:{ 'Cache-Control': '无缓存,无存储,必须重新验证', “杂注”:“no - cache”, “到期”:“0” } });