我有以下代码对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)

其他回答

如果希望在每个请求的基础上控制缓存,则必须使用更复杂的函数,如$.ajax()。或者,如果你只是想关闭它,把这个放在你的脚本顶部:

$.ajaxSetup ({
    // Disable caching of AJAX responses
    cache: false
});

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

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

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

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

你可以用缓存设置为false的版本替换jquery load函数。

(function($) {
  var _load = jQuery.fn.load;
  $.fn.load = function(url, params, callback) {
  if ( typeof url !== "string" && _load ) {
        return _load.apply( this, arguments );
  }
    var selector, type, response,
      self = this,
      off = url.indexOf(" ");

    if (off > -1) {
      selector = stripAndCollapse(url.slice(off));
      url = url.slice(0, off);
    }

    // If it's a function
    if (jQuery.isFunction(params)) {

      // We assume that it's the callback
      callback = params;
      params = undefined;

      // Otherwise, build a param string
    } else if (params && typeof params === "object") {
      type = "POST";
    }

    // If we have elements to modify, make the request
    if (self.length > 0) {
      jQuery.ajax({
        url: url,

        // If "type" variable is undefined, then "GET" method will be used.
        // Make value of this field explicit since
        // user can override it through ajaxSetup method
        type: type || "GET",
        dataType: "html",
        cache: false,
        data: params
      }).done(function(responseText) {

        // Save response for use in complete callback
        response = arguments;

        self.html(selector ?

          // If a selector was specified, locate the right elements in a dummy div
          // Exclude scripts to avoid IE 'Permission Denied' errors
          jQuery("<div>").append(jQuery.parseHTML(responseText)).find(selector) :

          // Otherwise use the full result
          responseText);

        // If the request succeeds, this function gets "data", "status", "jqXHR"
        // but they are ignored because response was set above.
        // If it fails, this function gets "jqXHR", "status", "error"
      }).always(callback && function(jqXHR, status) {
        self.each(function() {
          callback.apply(this, response || [jqXHR.responseText, status, jqXHR]);
        });
      });
    }

    return this;
  }
})(jQuery);

把这个放在全局的某个地方,它将在jquery加载后运行,你应该都设置好了。您现有的加载代码将不再被缓存。

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

下面是一个如何在每个请求的基础上控制缓存的示例

$.ajax({
    url: "/YourController",
    cache: false,
    dataType: "html",
    success: function(data) {
        $("#content").html(data);
    }
});