【问题标题】:How to prevent caching (force image reload) wtih PHP and JavaScript/jQuery如何使用 PHP 和 JavaScript/jQuery 防止缓存(强制图像重新加载)
【发布时间】:2011-10-04 10:48:59
【问题描述】:

我尝试了这两种方法来防止使用 PHP 和 JavaScript/jQuery 进行缓存(强制图像重新加载),但是当我通过 ajax 加载内容时,我仍然从缓存中获取图像,

PHP:

 // Generate a number that will never be repeated using the time function 
 // that "returns the current time measured in the number of seconds since 
 // the Unix Epoch (January 1 1970 00:00:00 GMT)"
 $cachekiller = time();

 // Include the generated number in the image URL
 <img src="path/to/image.png?{$cachekiller}"

Javascript/jquery:

// Generate random number between 1 and 1000.
var cachekiller = Math.floor(Math.random()*1001);

$("#thumbnail").attr("src", "path/to/image.png?"+cachekiller);

我还能做什么?

【问题讨论】:

  • 这不应该被缓存,因为每次获取它的 URL 都是唯一的。你如何确定它被缓存了?
  • 你是什么意思,通过ajax?
  • 您将哪些缓存标头与图像一起发送?
  • 大家好 - 我的意思是图像大小在服务器中经常变化,我想通过 ajax 重新加载它。

标签: php jquery image caching refresh


【解决方案1】:

我假设图像在服务器中经常更改,并且您想通过 ajax 重新加载它。如果是这种情况,向 URL 添加随机数将无济于事。这是因为,服务器不对基于 URL 的资源应用缓存控制。他们在请求标头中使用“If-Modified-Since”参数进行缓存控制。请参阅this。您可以尝试将请求中的 If-Modified-Since 标头设置为过去的时间,看看是否有帮助。

【讨论】:

  • 谢谢。是的 - 图像大小在服务器中经常变化,我想通过 ajax 重新加载它。
猜你喜欢
  • 2012-03-01
  • 2018-08-03
  • 2019-09-03
  • 2015-11-06
  • 1970-01-01
  • 2011-12-15
  • 2019-10-21
  • 2012-06-13
  • 1970-01-01
相关资源
最近更新 更多