如何缓存从一个Ajax调用接收到的数据? [英] How to cache data received from an Ajax call?

查看:188
本文介绍了如何缓存从一个Ajax调用接收到的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缓存从服务器接收的数据,以便有执行的PHP / MySQL的指令的最小数目。我知道缓存选项将自动为$。阿贾克斯()设置。但是,我看到的MySQL指令每次$。阿贾克斯()被调用,即使POSTDATA是一样的,因为它是在previous电话。我失去了一些东西?什么是缓存从服务器接收数据的最佳方式是什么?这是我的code:

  VAR POSTDATA = {'PID':'?< PHP的echo $ PROJECT_ID;>',
                record_id:this.id};

$阿贾克斯({
    键入:POST,
    网址:get_note.php
    数据:POSTDATA
})。完成(功能(数据){
    如果(数据!='0'){
        //添加对话框内容
        $('#note_container')的HTML(数据)。
        $('#note_container)对话框()。
    } 其他 {
        警报(woops);
    }
});
 

解决方案

这里的想法。它调整,当然您的需求。

 函数getAjaxData(){
    变量$节点= $('#note_container');
    如果($ node.data('AJAX缓存)。长度== 0){
        $阿贾克斯({
            //做的东西。
            成功:功能(数据){
                //添加对话框内容
                $ node.html(数据)的.data('AJAX缓存,数据).dialog();
            }
        });
    } 其他 {
        $ node.html($ node.data('AJAX缓存')).dialog();
    }
}
getAjaxData();
 

I would like to cache data received from the server so that there are a minimum number of PHP/MySQL instructions executed. I know that the cache option is automatically set for $.ajax(). However, I am seeing MySQL instructions every time $.ajax() is called even if postdata is the same as it was in a previous call. Am I missing something? What is the best way to cache data received from the server? Here is my code:

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);
    }
});

解决方案

Here's the idea. Tweak it to your needs, of course.

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();

这篇关于如何缓存从一个Ajax调用接收到的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆