1

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!

bignose
  • 30,281
  • 14
  • 77
  • 110
Willem
  • 1,094
  • 1
  • 12
  • 23

2 Answers2

6

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.

Aaron Maenpaa
  • 119,832
  • 11
  • 95
  • 108
mercator
  • 28,290
  • 8
  • 63
  • 72
  • More cache redirect stats can be found via [browserscope](http://www.browserscope.org/?category=network&v=top) – Markus Peröbner Feb 04 '13 at 16:10
  • @Markus, thanks! I updated my answer to link to Browserscope instead, and to describe the current situation rather than as it was 4 years ago. – mercator Feb 05 '13 at 11:55
1

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.

Community
  • 1
  • 1
ChristopheD
  • 112,638
  • 29
  • 165
  • 179