broswer가 몇 분 내에 서버를 쿼리 할 필요가 없도록 broswer에 데이터를 캐시하고 싶습니다. PHP 캐시 헤더를 추가했지만 작동하지 않는 것 같습니다. Ajax 코드 :Ajax가 캐시 할 수 없음
function myAjax(name, callback) {
var url = './php/getJson.php?name=' + encodeURIComponent(name) + '&callback=?';
jQuery.getJSON(url, function(data) {
callback(data);
jQuery.ajaxSetup({ cache: true });
});
}
PHP 코드 : MitchS 및 lenzai의 도움을
$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
$lm = gmdate("D, d M Y H:i:s", time() - $seconds_to_cache/2) . " GMT";
header("Last-Modified: $lm");
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: public, max-age=$seconds_to_cache");
include_once('getData.php');
$output = $_GET['name'];
echo $_GET['callback'].'('.$output.')';
덕분에 여기 내 아약스 코드와 PHP 코드입니다. 이 문제는 해결되었습니다. cache : true 옵션은 모든 ajax 쿼리와 오래된 jquery 라이브러리가 캐싱을 지원하기 전에 설정되어야합니다.
Ajax 코드 : 그래서 는 작업 예제를 원하는 사람들을위한 최신 jQuery 라이브러리
를 사용하고 있는지 확인
var callback = function(data) {
alert("callback");
}
function myAjax(name) {
var url = './php/getJson.php?name=' + encodeURIComponent(name) + '&callback=?';
jQuery.ajaxSetup({ cache: true });
jQuery.getJSON(url, function(data) {
callback(data);
});
}
PHP 코드 : 당신은
$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
$lm = gmdate("D, d M Y H:i:s", time() - $seconds_to_cache/2) . " GMT";
header("Last-Modified: $lm");
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: public, max-age=$seconds_to_cache");
$output = '{"eventList":["event1","test event"]}';
echo $_GET['callback'].'('.$output.')';
당신의 유효 기간이 –