【问题标题】:Why does the browser not cache a 301 within an AJAX-request?为什么浏览器不在 AJAX 请求中缓存 301?
【发布时间】:2010-10-16 01:36:21
【问题描述】:

这是 XMLHttpRequest:

$.ajax({
    method: "get",
    url: "getPage.php",
    data: $data,
    dataType: 'json',
    timeout: 2000,
    success: function(result) {
        handleContent(result);
        }
    });

这是 getPage.php?data=data

header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
header("Cache-Control: max-age=" . $offset . ", public");
header("HTTP/1.1 301 Moved Permanently");
header("Location: $location);

这是$位置:

header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
header("Cache-Control: max-age=" . $offset . ", public");
print $print;

客户端浏览器正确缓存 $location。但是它不会在 getPage.php?data=data

中缓存重定向

每次调用 ajax 请求时,它都会请求一个 GET getPage.php?data=data。

我希望它自动 GET $location(或者尝试 GET $location 并从缓存中获取页面)。

这不是 301 永久重定向的用途吗?创建一个被浏览器缓存的重定向(当然还有一些代理、搜索引擎等)?

请不要质疑我为什么选择这样做。我有这个原因,我不打算在这里讨论。我想要的只是一个答案,并且可能是一个解决方案,它可以让 301 重定向被缓存,导致在第一次请求之后根本没有 GET 请求。

提前致谢!

【问题讨论】:

    标签: php apache http-headers


    【解决方案1】:

    编辑:现在(2013 年 11 月)大多数浏览器都进行缓存重定向,请参阅 Browserscope(“缓存重定向”测试),但在提出问题时它们没有。

    【讨论】:

    • 更多缓存重定向统计信息可以通过browserscope找到
    • @Markus,谢谢!我更新了我的答案,改为链接到 Browserscope,并描述了当前的情况,而不是 4 年前的情况。
    【解决方案2】:

    这个线程和它的 cmets 包含处理这个确切问题的方法,尽管针对该问题提出的解决方案仍然有点“脆弱”(主要是因为目前的浏览器实现):

    How to manage a redirect request after a jQuery Ajax call

    希望您能从中受益。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多