var counter = 1;
$('button').click(function(){
$.ajax({
url: 'index.json',
method: 'GET',
success: function(res){
$('body').append(JSON.stringify(res))
if (!!(window.history && history.pushState)) {
history.replaceState(null, null, '?page=' + counter);
}
counter++;
}
});
});
网站上出现了一个门框,与以下事实有关:当单击按钮时,数据通过 ajax 加载,浏览器历史记录通过 replaceState 方法通过历史 api 更新,重写历史记录。
问题是,如果我按下后退按钮,则不会显示上一页,而是显示 ajax 请求的结果。也就是说,ajax 结果似乎写入了历史记录,尽管 url 是正确的
这是在 chrome 中,在 FF 中它工作正常,我可以看到整个页面。我试图在一个单独的 html 页面上重现它 - 所有浏览器中的一切都在嗡嗡作响 - 有人遇到过这个吗?
如果将 GET 请求改为 POST,则一切正常
就像历史记住了这个 GET 请求(只能在 chrome 中播放)
这是关于ajax的。它缓存获取请求。有帮助
cache: false