2011-07-10 260 views
0

我想使用$.post函数jquery做div刷新,只有当php脚本中的json数据返回的内容被修改时。我知道与$.post阿贾克斯调用永远不会被缓存。如果$.post或其他任何可能的方法都不可能,请帮我使用$.post$.ajaxjquery POST刷新div

谢谢

回答

6

为什么不缓存通话的响应?

var cacheData; 
$.post({..... 
     success: function(data){ 
        if (data !== cacheData){ 
        //data has changed (or it's the first call), save new cache data and update div 
        cacheData = data; 
        $('#yourdiv').html(data); 
        }else{ 
        //do nothing, data hasan't changed 

这只是一个例子,你要适应它以满足您的需求(和返回的数据结构)

+0

打我冲 –

0
var result; 
$.post({ 
    url: 'post.php' 
    data: {new:'data'} 
    success: function(r){ 
     if (result && result != r){ 
     //changed 
     } 

     result = r; 
    } 
}); 
0

你的问题是不完全清楚,但如何对这样的事情...

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#refresh").click(function(){ 
     info = ""; 
     $.getJSON('<URL TO JSON SOURCE>', function(data){ 
      if(info!=data){ 
      info = data; 
      $("#content").html(data); 
      } 
     }); 
     }); 
    }); 
    </script> 
    <div id="content"></div> 
    <input id="refresh" type="submit" value="Refresh" /> 

我认为你应该使用.getJSON()像我用它在那里,它的结构紧凑,并提供你需要的所有功能。

0
var div = $('my_div_selector'); 
function refreshDiv(data){ 
    // refresh the div and populate it with some data passed as arg 
    div.data('content',data); 
    // do whatever you want here 
} 
function shouldRefreshDiv(callback){ 
    // determines if the data from the php script is modified 
    // and executes a callback function if it is changed 
    $.post('my_php_script',function(data){ 
     if(data != div.data('content')) 
      callback(data); 
    });  
} 

,那么你可以在一个区间打电话shouldRefreshDiv(refreshDiv)也可以将其连接到一个事件处理