缓存AJAX请求 [英] Cache AJAX requests
问题描述
我发送Ajax GET-请求到PHP应用程序,并想缓存请求返回,供以后使用。
I am sending AJAX GET-requests to a PHP application and would like to cache the request returns for later use.
由于我使用GET这应该是可能的,因为不同的请求,要求不同的URL(如getHTML.php?页= 2,getHTML.php?页= 5)。
Since I am using GET this should be possible because different requests request different URLs (e.g. getHTML.php?page=2 and getHTML.php?page=5).
我需要在PHP应用程序,使用户的浏览器缓存以适当的方式请求URL的内容要申报什么头?我需要声明Javascript代码中任何事情处理AJAX请求(我使用jQuery的$就功能具有高速缓存参数)?
What headers do I need to declare in the PHP-application to make the clients browser cache the request URL content in a proper way? Do I need to declare anything in the Javascript which handles the AJAX-request (I am using jQuery's $.ajax function which has a cache parameter)?
我将如何处理其中的编辑,例如更改的内容getHTML.php?页= 2,这样客户端不回落到缓存的版本? 添加另一个参数的GET请求如getHTML.php页= 2及?因为链接请求的URL没有任何检查自动创建(这是preferably我希望它的方式)版本= 2是不可能的。
How would I handle edits which change the content of e.g. getHTML.php?page=2 so that the client doesn't fall back to the cached version? Adding another parameter to the GET request e.g. getHTML.php?page=2&version=2 is not possible because the link to the requested URL is created automatically without any checking (which is preferably the way I want it to be).
如何将浏览器反应过来的时候我尝试AJAX请求缓存请求URL?请问AJAX请求返回成功立刻?
How will the browser react when I try to AJAX-request a cached request URL? Will the AJAX-request return success immediately?
感谢
威廉
推荐答案
添加下列头的服务器上:
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.
此外,检查您的服务器不发出类似语用其他一些反缓存头。如果是的话,加上杂注:高速缓存头太
Also, check if your server do not issue some other anti-caching headers like Pragma. If so, add "Pragma: cache" header too.
这篇关于缓存AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!