Cache AJAX requests

后端 未结 2 1367
别跟我提以往
别跟我提以往 2020-12-15 00:52

I am sending AJAX GET-requests to a PHP application and would like to cache the request returns for later use.

Since I am using GET this should be possible because d

2条回答
  •  甜味超标
    2020-12-15 01:28

    Add the following headers on the server:

        header("Cache-Control: private, max-age=$seconds");
        header("Expires: ".gmdate('r', time()+$seconds));
    

    Where $seconds has an obvious meaning.

    We set an Expires header here even though it should be ignored if Cache-Control header with max-age parameter is set previously because there are clients that do not follow the standard.

    Also, check if your server do not issue some other anti-caching headers like Pragma. If so, add Pragma: cache header too.

提交回复
热议问题