为什么浏览器不缓存一个AJAX请求中的301? [英] Why does the browser not cache a 301 within an AJAX-request?

查看:311
本文介绍了为什么浏览器不缓存一个AJAX请求中的301?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是XMLHtt prequest:

This is the XMLHttpRequest:

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

这是getPage.php?数据=数据

This is 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;

客户端浏览器缓存正确位置$。然而,它并不缓存重定向getPage.php?数据=数据

The client browser properly caches $location. However it does not cache the redirect in getPage.php?data=data

每次AJAX请求被称为它请求一个GET getPage.php?数据=数据。

Every time the ajax-request is called it requests a GET getPage.php?data=data.

我想它会自动获得$位置,而不是(或者说尝试获得$位置,并获得缓存页)。

I would like it to automatically GET $location instead (or rather try GET $location and get the page from cache).

这是不是301永久重定向是?创建它是由浏览器缓存重定向(加上一些代理,当然,搜索引擎等东西)?

Is this not what 301 Permanent Redirect is for? Creating a redirect which is cached by the browser (plus some proxy, search engine etc. stuff of course)?

请不要质疑我为什么选择做这种方式。我有原因的,我我不打算在这里赘述了。我想要的是一个答案,可能的解决方案,它可以让导致没有GET请求第一个请求后在所有的301重定向获取缓存。

Please do not question why I choose to do it this way. I have reasons for this which I am not going to go into here. All I want is an answer and possibly a solution which lets the 301 redirect get cached resulting in no GET requests at all after a first request.

在此先感谢!

推荐答案

修改:大多数浏览器现在(2013年11月)做缓存重定向,看到的 Browserscope (以下简称高速缓存重定向测试),但他们并没有在当时有人问

Edit: most browsers now (November 2013) do cache redirects, see Browserscope (the "Cache Redirects" test), but they didn't at the time the question was asked.

这篇关于为什么浏览器不缓存一个AJAX请求中的301?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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