简体   繁体   English

收到http响应代码304后,如何解决缓存的内容刷新问题?

[英]How to get around the cached content refresh after http response code 304 is received?

I have a php application that uses prototypejs to do ajax polling with another php script, to check if there is new content or if content has been updated. 我有一个PHP应用程序,它使用prototypejs与另一个PHP脚本进行ajax轮询,以检查是否有新内容或内容是否已更新。 It returns a http response code of 304 if the content has not changed. 如果内容未更改,则它返回http响应代码304。

This all works fine, however i just found out that browsers seem to refresh the content eventhough a response code 304 was sent. 一切正常,但是我发现,即使发送了响应代码304,浏览器似乎仍在刷新内容。 It seems this data is coming from the browser cache, which was filled with content from the last succesful response code 200 (when the poller found changed data). 看来此数据来自浏览器缓存,缓存中填充了来自最后成功的响应代码200(当轮询器发现更改的数据时)的内容。

The http rfc states: "If the status code is 304 (Not Modified), the cache uses the entity- body stored in the cache entry as the entity-body of this outgoing response". http rfc指出:“如果状态代码为304(未修改),则高速缓存将使用存储在高速缓存条目中的实体作为该传出响应的实体。”

Is there a way to get around this? 有办法解决这个问题吗? Because eventhough no data was changed, people still see a slight flickering every time the code 304 is returned. 因为即使没有数据被更改,但是每次返回代码304时,人们仍然会看到轻微的闪烁。 I could use a response code 410 (Gone), which does not have this weird behaviour, but i don't think thats the way to do it. 我可以使用响应代码410(已消失),它没有这种怪异的行为,但是我认为这不是解决问题的方法。

My current ajax call is simple prototype code: 我当前的ajax调用是简单的原型代码:

function showProcessing() {
  if(Ajax.activeRequestCount > 0)
    document.getElementById('inProgress2').style.display = 'none';
    document.getElementById('inProgress').style.display = 'inline';
}

function hideProcessing () {
  if(Ajax.activeRequestCount <= 0)
    document.getElementById('inProgress').style.display = 'none';
    document.getElementById('inProgress2').style.display = 'inline'; 
}

Ajax.Responders.register({
  onCreate: showProcessing,
  onComplete: hideProcessing
});

So if this behaviour can be disabled via prototype, then it's probably something that has to be done at the 'onComplete' part, but i haven't got a clue how. 因此,如果可以通过原型禁用此行为,则可能必须在“ onComplete”部分执行此操作,但是我不知道如何操作。

请另外查看原始的相关网站http://codeigniter.com/wiki/AJAX_for_CodeIgniter/,因为它仅提供了有关htis的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM