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

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

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

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


当前回答

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

我创建了一个函数

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, ''));

我用的时候效果很好。

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

我创建了一个函数

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”); } }——

这段代码可能会对您有所帮助

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

另一种方法是只在需要从服务器获取数据时才添加下面的行,将下面的行与您的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;
}