Grails的:最好的方式发送缓存头与每一个Ajax调用 [英] Grails: best way to send cache headers with every ajax call

查看:120
本文介绍了Grails的:最好的方式发送缓存头与每一个Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是众所周知的 Internet Explorer的积极缓存Ajax调用而所有其他浏览器抢每次数据新鲜。这通常是不好:我从来没有遇到过,我想阿贾克斯联系不上服务器的情况。 Firefox,Safari和其他浏览器知道这一点,不要缓存Ajax调用。

It's well known that Internet Explorer aggressively caches ajax calls whereas all the other browsers grab the data fresh every time. This is usually bad: I've never encountered a case where I want ajax to NOT contact the server. Firefox, Safari and the other browsers know this and don't cache ajax calls.

要prevent IE从缓存中,你必须做下列之一:

To prevent IE from caching, you have to do one of the following:

  • 一个缓存阻止令牌添加到查询字符串(如?时间= [时间戳]
  • 发送一个HTTP响应头,专门禁止IE缓存的要求
  • 使用,而不是一个GET ajax的POST

我更preFER设置无缓存头。这是正确的方法:它告诉所有的浏览器不缓存,这正是你想要的。查询字符串的方法填补了浏览器的高速缓存,将永远不会被取出,留下合法的缓存内容的余地更小的东西。而POST方法是HTTP的腐败:帖子是修改数据。

I much prefer setting a no-cache header. It's the correct way: it tells all browsers not to cache, which is exactly what you intend. The query string method fills up the browser's cache with stuff that'll never be retrieved, leaving less room for legitimate cache content. And the POST method is a corruption of HTTP: POSTs are for modifying data.

在Grails的,什么是自动发送一个做不缓存头为所有Ajax请求的最佳方法是什么?我不想修改任何控制器,所以我想我们有了是一个很酷的过滤器恶作剧还是什么的。

In Grails, what's the best way to automatically send a do-not-cache header for all ajax requests? I don't want to modify any controllers, so I'm thinking there's got to be a cool filter trick or something.

谢谢!

推荐答案

这就是我终于想通了。大多数JavaScript库--including的jQuery,YUI,Mootools的和原型 - 发送 X-要求 - 由于:XmlHtt prequest 在每个AJAX请求头。

Here's what I finally figured out. Most javascript libraries --including jQuery, YUI, Mootools and Prototype -- send the X-Requested-With: XmlHttpRequest header on every ajax request.

有关发送这个头的任何要求,你可以发送一个响应头回来,告诉它不缓存。

For any request that sends this header, you can send a response header back that tells it to not cache.

下面是一个Grails筛选确定自己与 X-请求,使用Ajax请求的prevents缓存:XmlHtt prequest 标题:

Below is a Grails filter that prevents caching of ajax requests that identify themselves with the X-Requested-With: XmlHttpRequest header:

// put this class in grails-app/config/
class AjaxFilters {
    def filters = {
        all(controller:'*', action:'*') {
            before = {
                if (request.getHeader('X-Requested-With')?.equals('XMLHttpRequest')) {
                    response.setHeader('Expires', '-1')
                }
            }
        }
    }
}

有些人preFER使用缓存控制:无缓存,而不是头到期。这里的区别:

Some people prefer to use the Cache-Control: no-cache header instead of expires. Here's the difference:

  • 的Cache-Control:no-cache的 - 绝对没有缓存
  • 到期日:-1 - 浏览器的通常的接触,通过Web服务器更新到网页有条件的If-Modified-Since请求。但是,页保留在磁盘高速缓存,并用于在适当的场合,而不远程Web服务器联系,当后退和前进按钮用于访问导航历史或当浏览器是在离线模式下,例如
  • Cache-Control: no-cache - absolutely NO caching
  • Expires: -1 - the browser "usually" contacts the Web server for updates to that page via a conditional If-Modified-Since request. However, the page remains in the disk cache and is used in appropriate situations without contacting the remote Web server, such as when the BACK and FORWARD buttons are used to access the navigation history or when the browser is in offline mode.

通过添加此过滤器,你做什么Firefox和Safari浏览器已经在做IE浏览器的缓存一致。

By adding this filter, you make Internet Explorer's caching consistent with what Firefox and Safari already do.

顺便说一句,我已经经历了IE8和IE9的缓存问题。我认为存在IE7和IE6和问题。

BTW, I've experienced the caching problem on IE8 and IE9. I assume the problem existed for IE7 and IE6 as well.

这篇关于Grails的:最好的方式发送缓存头与每一个Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆