问题
This is the XMLHttpRequest:
$.ajax({
method: "get",
url: "getPage.php",
data: $data,
dataType: 'json',
timeout: 2000,
success: function(result) {
handleContent(result);
}
});
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);
This is $location:
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
header("Cache-Control: max-age=" . $offset . ", public");
print $print;
The client browser properly caches $location. However it does not cache the redirect in getPage.php?data=data
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).
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)?
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.
Thanks in advance!
回答1:
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.
回答2:
This thread and it's comments contain ways to handle this exact problem, although the presented solutions for the problem still steem a little 'flakey' (mostly because of browser implementations at the moment):
How to manage a redirect request after a jQuery Ajax call
I hope you find it informative.
来源:https://stackoverflow.com/questions/691877/why-does-the-browser-not-cache-a-301-within-an-ajax-request