【发布时间】:2014-06-18 08:28:32
【问题描述】:
我在我的模板中使用以下 javascript 来刷新图像:
<script type='text/javascript'>
$(document).ready(function() {
var $img = $('#imageactual');
setInterval(function() {
$.get('{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}?cachebuster='+Math.floor(Math.random()*100 +1), function(data) {
var $loader = $(document.createElement('img'));
$loader.one('load', function() {
$img.attr('src', $loader.attr('src'));
});
$loader.attr('src', data);
if($loader.complete) {
$loader.trigger('load');
}
});
}, 2000);
});
</script>
我使用以下 html 代码显示图像:
<img id="imageactual" src="{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}" />
在我看来,我添加了
@never_cache
@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
和
response = render(request, 'mainsite/modify.html', locals(), context_instance=RequestContext(request))
response['Cache-Control'] = 'no-store, no-cache, must-revalidate proxy-revalidate'
response['Expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT'
response['Pragma'] = 'no-cache'
return response
但图像仍在浏览器中缓存(Chrome:版本 35.0.1916.153 m)...您知道如何避免这种情况吗?
编辑 1:
**Header:**
Remote Address:127.0.0.1:80
Request URL:http://127.0.0.1/medias/thumbs/odbgelguelteeglndjssastj.jpg
Request Method:GET
Status Code:200 OK (from cache)
【问题讨论】:
-
这些响应标头仅与网页相关,与图像无关。该图像由另一个请求提供,并且该响应具有自己的标头。我认为您需要修改给定目录/文件类型的网络服务器设置。
标签: javascript ajax django image browser-cache