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

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

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

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


当前回答

另一种方法是只在需要从服务器获取数据时才添加下面的行,将下面的行与您的ajax url一起附加。

' ? _ = ' + Math。round (Math . random () * 10000)

其他回答

/**
 * Use this function as jQuery "load" to disable request caching in IE
 * Example: $('selector').loadWithoutCache('url', function(){ //success function callback... });
 **/
$.fn.loadWithoutCache = function (){
 var elem = $(this);
 var func = arguments[1];
 $.ajax({
     url: arguments[0],
     cache: false,
     dataType: "html",
     success: function(data, textStatus, XMLHttpRequest) {
   elem.html(data);
   if(func != undefined){
    func(data, textStatus, XMLHttpRequest);
   }
     }
 });
 return elem;
}

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

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

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

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

如果你想坚持使用Jquery的.load()方法,可以在URL中添加一些独特的东西,比如JavaScript时间戳。“+新日期().getTime()”。注意,我必须添加一个“&time=”,这样它就不会改变pid变量。

$('#searchButton').click(function() {
$('#inquiry').load('/portal/?f=searchBilling&pid=' + $('#query').val()+'&time='+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”); } }——