Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 jQuery 在网页上动态加载图像。每次用户在页面上点击提交时,都会生成一个新图像。问题是,除非我在图像 URL 的末尾附加一个随机数,否则由于缓存而返回相同的图像。
我的代码看起来像这样......
img.attr('src', url + resp.id + '.png?' + cache);
我想在最后摆脱缓存字符串并仍然提供新图像,有人知道该怎么做吗?我不认为这是 HTTP 服务器端的问题(使用 nginx),因为即使没有缓存破坏器,直接请求也会显示正确的图像。
这是您可以规避此问题的最简单方法,因为它是设计的。
正常的做法是在字符串中附加时间
var d = new Date(); var cache = d.getTime(); // getTime() Returns the number of milliseconds since midnight Jan 1, 1970