使用 AJAX 时清除 IE 缓存,而不使用缓存清除查询字符串,但使用 http 响应标头
我遇到了经典的 IE-caches-everything-in-Ajax 问题。我有一些每分钟刷新的数据。
研究了论坛后,解决方案归结为以下选项(http://stackoverflow.com/questions/5997857/grails-best-way-to-send-cache-headers-with-every-ajax-call):
- 添加缓存-将令牌破坏到查询字符串(如?time=[timestamp])
- 发送一个HTTP响应标头,专门禁止IE缓存请求
- 使用ajax POST而不是GET
不幸的是,显而易见的querysting 或“cache: false”设置对我不起作用,因为更新的数据文件托管在 Akamai Netstorage 上并且无法接受查询字符串。我也不想使用 POST。
我想要做的是尝试发送一个专门禁止 IE 缓存请求的 HTTP 响应标头或者是否有其他人知道另一个缓存清除解决方案?
有谁知道如何做到这一点?任何帮助将不胜感激。
这是我的代码:
(function ($) {
var timer = 0;
var Browser = {
Version: function () {
var version = 999;
if (navigator.appVersion.indexOf("MSIE") != -1) version = parseFloat(navigator.appVersion.split("MSIE")[1]);
return version;
}
}
$.fn.serviceboard = function (options) {
var settings = { "refresh": 60};
return this.each(function () {
if (options) $.extend(settings, options);
var obj = $(this);
GetLatesData(obj, settings.refresh);
if (settings.refresh > 9 && Browser.Version() > 6) {
timer = setInterval(function () { GetLatestData(obj, settings.refresh) }, settings.refresh * 1000);
}
});
};
function GetLatestData(obj, refresh) {
var _url = "/path/updated-data.htm";
$.ajax({
url: _url,
dataType: "html",
complete: function () {},
success: function (data) {
obj.empty().append(data);
}
}
});
}
})(jQuery);
I'm having the classic IE-caches-everything-in-Ajax issue. I have a bit of data that refreshes every minute.
Having researched the forums the solutions boil down to these options (http://stackoverflow.com/questions/5997857/grails-best-way-to-send-cache-headers-with-every-ajax-call):
- add a cache-busting token to the query string (like ?time=[timestamp])
- send a HTTP response header that specifically forbids IE to cache the request
- use an ajax POST instead of a GET
Unfortunately the obvious querysting or "cache: false" setting will not work for me as the updated data file is hosted on Akamai Netstorage and cannot accept querystrings. I don't want to use POST either.
What I want to do is try send an HTTP response header that specifically forbids IE to cache the request or if anyone else knows another cache busting solution??
Does anyone know how this might be done? Any help would be much appreciated.
Here is my code:
(function ($) {
var timer = 0;
var Browser = {
Version: function () {
var version = 999;
if (navigator.appVersion.indexOf("MSIE") != -1) version = parseFloat(navigator.appVersion.split("MSIE")[1]);
return version;
}
}
$.fn.serviceboard = function (options) {
var settings = { "refresh": 60};
return this.each(function () {
if (options) $.extend(settings, options);
var obj = $(this);
GetLatesData(obj, settings.refresh);
if (settings.refresh > 9 && Browser.Version() > 6) {
timer = setInterval(function () { GetLatestData(obj, settings.refresh) }, settings.refresh * 1000);
}
});
};
function GetLatestData(obj, refresh) {
var _url = "/path/updated-data.htm";
$.ajax({
url: _url,
dataType: "html",
complete: function () {},
success: function (data) {
obj.empty().append(data);
}
}
});
}
})(jQuery);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 GET 请求中添加一个随机数,这样 IE 就不会在其缓存中将其识别为“相同”。这个数字可以是时间戳:
编辑也许可以生成请求的网址:
我认为这不会导致任何错误。
EDIT2 抱歉,我刚刚更好地阅读了您的帖子,发现这不适合您。
您说“托管在 Akamai 上,无法接受查询字符串”,但为什么不呢?
我从未听说过哪个页面不接受附加的:“?blabla”,即使它是 html。
Add a random number to the GET request so that IE will not identify it as "the same" in its cache. This number could be a timestamp:
EDIT perhaps make the requested url:
This shouldn't cause any errors I believe.
EDIT2 Sorry I just read your post a bit better and saw that this is not an option for you.
You say "is hosted on Akamai and cannot accept querystrings" but why not?
I've never heard of a page that won't accept an additional: "?blabla", even when it's html.
这让我发疯。我尝试了许多缓存清除技术并设置缓存头。其中许多要么不起作用,要么是徒劳无功。我发现的唯一经过测试可以正常工作的解决方案是设置:
我希望它可以帮助其他人解决 IE 问题。
This was driving me crazy. I tried many cache busting techniques and setting cache headers. So many of these either did not work or were wild goose chases. The only solution I found which tested to work correctly was setting:
I hope it saves others with IE headaches.