0

为什么我的 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
4

1 回答 1

2

我发现了问题:

1.) http://api.jquery.com/jQuery.ajax/上的“ifModified” 在我看来是绝对错误的。在阅读了 jQuery源代码之后,我将删除这句话:

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

并且会这样写:如果“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();
} 
于 2013-02-12T11:34:38.443 回答