Django - Javascript -禁用图像刷新缓存
Django - Javascript - Disable cache on image refresh
在我的模板中,我使用以下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: Version 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)
正如@yedpodtrzitko指出的那样,在Django中调整web页面的缓存头是没有意义的。
这部分看起来不对:
$.get('{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}?cachebuster='+Math.floor(Math.random()*100 +1), function(data) {
{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}
是要显示的图片的url,对吗?(在html img
标签中使用相同的url)
在这种情况下,通过ajax加载url是没有意义的。
看起来你根本不需要ajax,你只需要定期更新img
标签的src
属性,附带cachebuster查询字符串:
<script type='text/javascript'>
$(document).ready(function() {
var $img = $('#imageactual');
setInterval(function() {
$img.attr('src', '{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}?cachebuster='+Math.floor(Math.random()*100 +1));
}, 2000);
});
</script>
相关文章:
- 如何在动态表更新后刷新整个表排序缓存
- Firebug 不会刷新更改后的 JavaScript 上的缓存
- 在 js/css/html 更改 Web 应用程序上强制刷新缓存
- 如何刷新PDF.js查看器的缓存
- 当我“拉取刷新”时,如何清理缓存?使用Ionic Framework和AngularJS
- 网站只有在Chrome上缓存刷新时才能完全运行,在其他浏览器上运行良好
- 如何强制 jQuery 向服务器发出 GET 请求以刷新缓存的资源
- 预加载图片以进行刷新、缓存
- 离子 / 如何期望刷新缓存视图
- 刷新访问者的缓存
- 为对事件更新文件的相同调用刷新浏览器缓存
- 刷新页面时使用chrome缓存的图像.load()工作不正常
- 修改head.js javascript调用后强制刷新缓存
- backbone.js缓存集合并刷新
- 刷新JSP/避免缓存中的图像
- Django - Javascript -禁用图像刷新缓存
- 使用站点更新刷新缓存
- PHP重定向后图像不刷新(缓存问题?)
- Javascript重定向页面和刷新缓存在同一时间
- 使用?version=XXX刷新缓存中的CSS文件