【问题标题】:jQuery, ajax wont cachejQuery,ajax不会缓存
【发布时间】:2013-02-12 06:31:40
【问题描述】:

为什么我的 jQuery ajax 不支持ETag 功能?

您在第二个示例中看到,没有“If-None-Match”标头。但为什么呢?

在两个经过测试的浏览器中,我有一个 50% 的变化,有一个缓存将按 F5,但只需通过单击链接重新打开页面,它就不起作用了。

不工作的 jQuery 示例

var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        console.log( xmlhttp.responseText );
    }
}
xmlhttp.open("GET","/energyManagerSensor/getCleanData/sensor_id/4?shrink_type=day&from=18.11.2011&to=23.02.2013",true);
xmlhttp.send();

请求标头

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,de-CH;q=0.6,fr-FR;q=0.5,fr;q=0.4,en-gb;q=0.3,sr-RS;q=0.2,sr;q=0.1
Cache-Control   max-age=0
Connection  keep-alive
Cookie  PHPSESSID=iie173rsc5bqggll6uhmg6m7i6
Host    sfportal3_hh_dev
If-Modified-Since   Tue, 12 Feb 2013 0:0:0 GMT
If-None-Match   4SdayF1321570800T1361574000
Referer http://sfportal3_hh_dev/energyManagerDisplay/view/display_id/1
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0 FirePHP/0.7.1
x-insight   activate

响应标题

Cache-Control   max-age=3600
Connection  close
Date    Tue, 12 Feb 2013 06:16:23 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Server  Apache/2.2.22 (Ubuntu)

不工作的 jQuery 示例

$.ajax({
    url: "/energyManagerSensor/getCleanData/sensor_id/4?shrink_type=day&from=18.11.2011&to=23.02.2013",
    type: 'GET',
    dataType: "json",
    cache: true,
    ifModified: true,
    success: function(return_data) {
    console.log(return_data);
    }
});

请求标头

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,de-CH;q=0.6,fr-FR;q=0.5,fr;q=0.4,en-gb;q=0.3,sr-RS;q=0.2,sr;q=0.1
Connection  keep-alive
Cookie  PHPSESSID=iie173rsc5bqggll6uhmg6m7i6
Host    sfportal3_hh_dev
Referer http://sfportal3_hh_dev/energyManagerDisplay/view/display_id/1
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0 FirePHP/0.7.1
X-Requested-With    XMLHttpRequest
x-insight   activate

响应标题

Cache-Control   max-age=3600
Connection  Keep-Alive
Content-Type    text/json
Date    Tue, 12 Feb 2013 06:18:41 GMT
Etag    5SdayF1321570800T1361574000
Expires Tue, 12 Feb 2013 07:02:18 GMT
Keep-Alive  timeout=5, max=74
Last-Modified   Tue, 12 Feb 2013 0:0:0 GMT
Server  Apache/2.2.22 (Ubuntu)
Transfer-Encoding   chunked
X-Powered-By    PHP/5.3.10-1ubuntu3

测试环境

  • 火狐18.0.2
  • 铬 24.0

【问题讨论】:

  • 你测试的是哪个浏览器?

标签: jquery ajax browser-cache


【解决方案1】:

我发现了问题:

1.) http://api.jquery.com/jQuery.ajax/ 上的“ifModified” 在我看来是绝对错误的。 看完jQuerysource code,我会去掉那句话:

“仅当响应自上次请求后发生更改时才允许请求成功。”

并且会写如下内容: 如果 "ifModified" 为真,则将使用 lastModified 和 etag 标头选项进行主动缓存。

2.) 我在 PHP 中遇到了问题

需要始终发送 ETag,在 304 响应的情况下也是如此。

<?php
// Save performance use ETag system 
// @see http://en.wikipedia.org/wiki/HTTP_ETag
$lastmod = gmdate('D, d M Y 0:0:0 \G\M\T', time());
$etag = $sensor_id . 'S' . $shrink_type . 'A' . $alarms . 'F' . $from . 'T' . $to . 'D' . date('Dmy');

$ifmod = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $lastmod : null; 
$iftag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] == $etag : null; 

header_remove('Pragma');
header_remove('Cache-Control');
header('Expires: ' . gmdate('D, d M Y H:m:i \G\M\T', time() + 3600));
header('Cache-Control: max-age=3600');
header('Last-Modified: ' . $lastmod); 
header('ETag: ' . $etag);

if (($ifmod || $iftag) && ($ifmod !== false && $iftag !== false)) { 
    header('HTTP/1.0 304 Not Modified'); 
    die();
} 

【讨论】:

  • 我不明白。解决方案是什么?你原来不是有 jquery 问题吗?
  • 我的问题是 jquery.ajax 的“ifModified”文档。由于缺少了解文档的原因,我更改了我的 php 代码。此选项需要为真才能强制 jQuery 使用提供的 HTTP ETag 标头。
  • 非常感谢您!任何错过它的人的关键学习是:如果您的服务器返回 304 记得附加一个 ETag 到它,否则 jQuery 不会使用缓存的响应。
猜你喜欢
  • 2015-01-12
  • 2022-07-06
  • 1970-01-01
  • 2018-05-08
  • 2014-01-07
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 2013-02-18
相关资源
最近更新 更多