强制AJAX请求重新验证与服务器的高速缓存,而不需要重新装载完全 [英] Forcing AJAX request to revalidate cache with server, without reloading completely

查看:209
本文介绍了强制AJAX请求重新验证与服务器的高速缓存,而不需要重新装载完全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,可以让浏览器缓存AJAX请求导致很长一段时间。我已经找到了如何使绕过缓存完全,当检测到可能的修改要求。但我想,让用户触发数据刷新。

I have a web application that lets the browser cache AJAX requests result for a long time. I have found out how to make a request that bypasses the cache entirely, when probable modifications are detected. But I would want to let the user trigger a data refresh.

在这种情况下,我想浏览器检查服务器是否缓存被冻结的,但使用它,如果它不是(即,如果服务器以304 $ C响应$ C)。我们的目标是做足了加载时间,因为数据是巨大的。

In this scenario, I'd like the browser to check with the server if the cache is stalled but use it if it is not (that is, if the server responds with a 304 code). The goal is to spare the loading time because the data is huge.

该服务器包含所有响应以下标题:

The server includes the following headers in all responses:

Cache-Control: private, max-age=604800
Last-Modified: ... # actual last modification date

我设法使用下面的HTTP头请求爆裂完全在浏览器(没有测试其他浏览器还)缓存的对象:

I managed to burst the cached object entirely in Chrome (not tested other browsers yet) by using the following HTTP headers in the request:

Cache-Control: max-age=0
If-Last-Modified: Tue, 01 Jan 1970 01:00:00 +0100

如果-的Last-Modified 行是一个真正有效果。 Chrome浏览器似乎忽略请求中的的Cache-Control 头。

The If-Last-Modified line is the one that really has an effect. Chrome seems to ignore the Cache-Control header in the request.

我还发现,使用的Cache-Control:必重新验证在服务器响应强制浏览器验证它与服务器的高速缓存的每个的要求。

I have also found that using Cache-Control: must-revalidate in the server response forces the browser to validate its cache with the server for each request.

但是,有什么办法可以重新验证了刚才的一个precise的要求,决定在客户端

But is there any way to revalidate for just one precise request, decided on the client-side?

请注意,我不是特别对用HTTP头做这个,所以任何其他方法,我将不知道的欢迎!

Note that I'm not specially attached to doing this with HTTP headers, so any other method that I would not be aware of is welcome!

推荐答案

您可以在时间来清理缓存只有一个precise请求添加URL参数,该参数值的基础。

you can add a url parameter which value base on time to clean cache for just one precise request.

$.ajax({
    url:"/questions?nocache="+Date.now(),
    "success":function(data){
         console.log(data);
    }
});

这篇关于强制AJAX请求重新验证与服务器的高速缓存,而不需要重新装载完全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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