![](/img/trans.png)
[英]Prevent jQuery .clone() from disappearing after page changes/refreshes
[英]Prevent caching after page refreshes every x seconds
我正在建立一个新闻应用程序。 我每10秒用.new_feed
类刷新一个div
,检查新的更新,以及何时显示。 现在的问题是,当有一个新的Feed,并且10秒钟启动,你没有点击查看,等待40秒再点击之前,它会带来4条记录而不是1条。我认为问题与缓存有关。
$(document).ready(function() {
$.ajaxSetup({ cache: false });
setInterval(function() {
$('.new_feed').load('headline.asp');
}, 10000);
});
$(function() {
//More Button
$('.more2').live("click",function() {
var u_pic_id = $(this).attr("id");
if (u_pic_id) {
$("#more2"+u_pic_id).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "show_more.asp",
data: "lastmsg="+ id,
cache: false,
success: function(html) {
$("ol#updates2").append(html);
$("#more2"+id).remove();
}
});
}
return false;
});
});
</script>
<div id="more2<%=(rs_ncount_id.Fields.Item("id").Value)+1%>" class="morebox2">
<a href="#" class="more2" id="<%=(rs_ncount_id.Fields.Item("id").Value)+1%>">
Load New Feeds
</a>
</div>
您必须在服务器端设置Cache-Control
HTTP/1.1
标头。 这是推荐的方法。
但是,如果无法更改服务器设置,也可以使用hacky解决方案。 在请求中使用nonce:
$('.new_feed').load('headline.asp?' + nonce);
因为它看起来像是对浏览器的不同请求,所以它将忽略缓存的值。
nonce最简单的解决方案是使用当前时间:
var date = new Date();
var nonce = date.getMilliseconds();
我写了一个答案,指出$.ajax
的cache:false
选项,但是在查看您的OP时,我意识到您已经在使用它。 文档指出cache:false
选项将仅在GET
请求(和IE8的POST
请求)上附加时间戳。
由于您使用的是POST
不太可能使用cache:false
选项实际上会有所帮助(除非您使用的是IE8?)。 相反,像@meskobalazs所说,你需要创建和附加一个随机数 。
实现这一点的一种方法可能是:
function getValues(id) {
var url = 'myUrl?id=' + id;
$.ajax({
type: "POST",
url: url + '&_=' + (new Date()).getTime()
}).done(function(response) {
// do stuff
})
}
当然,如果您有权访问服务器,则处理此问题的适当方法将是正确设置响应头。 您还可以考虑使用更加RESTful的方法,其中GET
动词和路由用于请求数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.