2011-07-10 138 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)也可以將其連接到一個事件處理