在 AngularJS 中防止 IE 缓存的更好方法? [英] Better Way to Prevent IE Cache in AngularJS?
问题描述
我目前使用 service/$resource 进行 ajax 调用(在本例中为 GET),并且 IE 缓存调用,以便无法从服务器检索新数据.我使用了通过谷歌搜索发现的一种技术来创建一个随机数并将其附加到请求中,这样 IE 就不会去缓存数据.
I currently use service/$resource to make ajax calls (GET in this case), and IE caches the calls so that fresh data cannot be retrieved from the server. I have used a technique I found by googling to create a random number and append it to the request, so that IE will not go to cache for the data.
有没有比为每个请求添加 cacheKill 更好的方法?
Is there a better way than adding the cacheKill to every request?
工厂代码
.factory('UserDeviceService', function ($resource) {
return $resource('/users/:dest', {}, {
query: {method: 'GET', params: {dest: "getDevicesByUserID"}, isArray: true }
});
来自控制器的调用
$scope.getUserDevices = function () {
UserDeviceService.query({cacheKill: new Date().getTime()},function (data) {
//logic
});
}
推荐答案
根据 binarygiant 的要求,我发布了我的评论作为答案.我通过在服务器端的响应中添加 No-Cache 标头解决了这个问题.请注意,您必须仅对 GET 请求执行此操作,其他请求似乎可以正常工作.
As binarygiant requested I am posting my comment as an answer. I have solved this problem by adding No-Cache headers to the response on server side. Note that you have to do this for GET requests only, other requests seems to work fine.
binarygiant 发布了如何在 node/express 上执行此操作.你可以在 ASP.NET MVC 中这样做:
binarygiant posted how you can do this on node/express. You can do it in ASP.NET MVC like this:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult Get()
{
// return your response
}
这篇关于在 AngularJS 中防止 IE 缓存的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!