我想使用$.post
函数jquery做div刷新,只有当php脚本中的json数据返回的内容被修改时。我知道与$.post
阿贾克斯调用永远不会被缓存。如果$.post
或其他任何可能的方法都不可能,请帮我使用$.post
或$.ajax
。jquery POST刷新div
谢谢
我想使用$.post
函数jquery做div刷新,只有当php脚本中的json数据返回的内容被修改时。我知道与$.post
阿贾克斯调用永远不会被缓存。如果$.post
或其他任何可能的方法都不可能,请帮我使用$.post
或$.ajax
。jquery POST刷新div
谢谢
为什么不缓存通话的响应?
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
这只是一个例子,你要适应它以满足您的需求(和返回的数据结构)
var result;
$.post({
url: 'post.php'
data: {new:'data'}
success: function(r){
if (result && result != r){
//changed
}
result = r;
}
});
你的问题是不完全清楚,但如何对这样的事情...
<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()
像我用它在那里,它的结构紧凑,并提供你需要的所有功能。
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)
也可以将其连接到一个事件处理
打我冲 –