Ste*_*305 5 ajax jquery caching
我想缓存从服务器收到的数据,以便执行最少数量的PHP/MySQL指令.我知道为$ .ajax()自动设置缓存选项.但是,即使postdata与之前的调用相同,每次调用$ .ajax()时,我都会看到MySQL指令.我错过了什么吗?缓存从服务器收到的数据的最佳方法是什么?这是我的代码:
var postdata = {'pid':'<?php echo $project_id;?>',
'record_id':this.id};
$.ajax({
type: "POST",
url: "get_note.php",
data: postdata
}).done(function(data){
if (data != '0') {
// Add dialog content
$('#note_container').html(data);
$('#note_container').dialog();
} else {
alert(woops);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的想法。当然,根据您的需要进行调整。
function getAjaxData(){
var $node = $('#note_container');
if ($node.data('ajax-cache').length == 0) {
$.ajax({
// do stuff.
success: function(data){
// Add dialog content
$node.html(data).data('ajax-cache',data).dialog();
}
});
} else {
$node.html( $node.data('ajax-cache') ).dialog();
}
}
getAjaxData();
Run Code Online (Sandbox Code Playgroud)