我有以下代码对URL进行GET请求:

$('#searchButton').click(function() {
    $('#inquiry').load('/portal/?f=searchBilling&pid=' + $('#query').val());            
});

但是返回的结果并不总是被反映出来。例如,我对输出堆栈跟踪的响应进行了更改,但是当我单击搜索按钮时,堆栈跟踪没有出现。我查看了控制ajax响应的底层PHP代码,它有正确的代码,直接访问页面显示了正确的结果,但.load返回的输出是旧的。

如果我关闭浏览器并重新打开它,它会工作一次,然后开始返回陈旧的信息。我可以通过jQuery控制这一点,还是我需要有我的PHP脚本输出头来控制缓存?


当前回答

一种方法是在url的末尾添加一个唯一的数字:

$('#inquiry').load('/portal/?f=searchBilling&pid=' + $('#query').val()+'&uid='+uniqueId());

在这里,您编写uniqueId()以在每次调用时返回不同的内容。

其他回答

这在IE中是一个特别烦人的问题。基本上你必须从服务器发送“无缓存”的HTTP头和响应。

不要使用时间戳来创建一个唯一的URL,因为你访问的每个页面都是由jquery移动缓存在DOM中,你很快就会遇到移动内存不足的麻烦。

$jqm(document).bind('pagebeforeload', function(event, data) {
    var url = data.url;
    var savePageInDOM = true;

    if (url.toLowerCase().indexOf("vacancies") >= 0) {
        savePageInDOM = false;
    }

    $jqm.mobile.cache =  savePageInDOM;
})

这段代码在页面加载之前激活,您可以使用URL . indexof()来确定该URL是否是您想要缓存的URL,并相应地设置缓存参数。

不要使用窗户。Location = "";要改变URL,否则你将导航到地址和pagebeforeload不会火。为了解决这个问题,只需使用window.location.hash = "";

对于PHP,添加这一行到你的脚本,它提供了你想要的信息:

header("cache-control: no-cache");

或者,在查询字符串中添加一个唯一的变量:

"/portal/?f=searchBilling&x=" + (new Date()).getTime()

萨沙是个好主意,我用搅拌机。

我创建了一个函数

LoadWithoutCache: function (url, source) {
    $.ajax({
        url: url,
        cache: false,
        dataType: "html",
        success: function (data) {
            $("#" + source).html(data);
            return false;
        }
    });
}

并调用我的页面的不同部分,例如在init:

Init: function (actionUrl1, actionUrl2, actionUrl3) {

var exampleJS= {

Init: function (actionUrl1, actionUrl2, actionUrl3)           ExampleJS.LoadWithoutCache(actionUrl1, "div1");

ExampleJS。LoadWithoutCache (actionUrl2 div2”); ExampleJS。LoadWithoutCache (actionUrl3 div3”); } }——

试试这个:

$("#Search_Result").load("AJAX-Search.aspx?q=" + $("#q").val() + "&rnd=" + String((new Date()).getTime()).replace(/\D/gi, ''));

我用的时候效果很好。