jQuery,ajax不会缓存

jQuery,ajax不会缓存,jquery,ajax,browser-cache,Jquery,Ajax,Browser Cache,为什么我的jqueryajax不支持这个特性 您可以在第二个示例中看到,没有“If None Match”标题。但是为什么呢 在这两个测试的浏览器中,我有50%的改变,有一个缓存将按F5,但在简单地重新打开页面点击链接它不会工作 无工作jQuery示例 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest();

为什么我的jqueryajax不支持这个特性

您可以在第二个示例中看到,没有“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();
$.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  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
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  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)
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
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();
$.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  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
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  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)
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
测试环境

  • Firefox18.0.2
  • 铬24.0
我发现了问题:

1.) 上的“ifModified” 在我看来这是完全错误的。 在阅读jQuery之后,我将删除以下句子:

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

我会写下这样的话: 如果“ifModified”为true,则将使用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代码。此选项必须为true才能强制jQuery使用提供的HTTP ETag头。非常感谢!对于错过它的人来说,关键是:如果您的服务器返回304,请记住在其上附加ETag,否则jQuery不会使用缓存响应。