浏览器返回后jquery获取并不会触发

时间:2014-12-09 15:30:49

标签: javascript jquery ajax asp.net-mvc

简化,在我的第一页中,我有这个:

$(function () {
    check();
})
function check() {
    $.get("/MyController/MyAction/", function (data) {
        if(data != undefined && data != "")
        {
            window.location.href = data;
        }
    })
}

这很有效,但是如果用户回到页面上(使用浏览器返回按钮),则jquery get函数不会到达控制器并且数据变量为空。 为什么jquery获取函数不执行我的控制器操作? 我使用ajax得到了相同的结果:

$.ajax({
    url: "/MyController/MyAction/",
})
.done(function (data) {
    if(data != undefined && data != "")
    {
        window.location.href = data;
    }     
});

1 个答案:

答案 0 :(得分:3)

当您使用后退按钮时,大多数浏览器都会从缓存中加载页面。

为防止这种情况,您可以使用Cache-Control HTTP标头:

Cache-Control: no-cache, max-age=0, must-revalidate, no-store

请参阅How to Defeat the Browser Back Button CacheHTTP Caching FAQ