Stop jQuery .load response from being cached

前端 未结 14 1884
迷失自我
迷失自我 2020-11-22 03:54

I have the following code making a GET request on a URL:

$(\'#searchButton\').click(function() {
    $(\'#inquiry\').load(\'/portal/?f=searchBilling&pid=         


        
相关标签:
14条回答
  • 2020-11-22 04:36
    /**
     * 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;
    }
    
    0 讨论(0)
  • 2020-11-22 04:37

    Here is an example of how to control caching on a per-request basis

    $.ajax({
        url: "/YourController",
        cache: false,
        dataType: "html",
        success: function(data) {
            $("#content").html(data);
        }
    });
    
    0 讨论(0)
  • 2020-11-22 04:38

    Try this:

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

    It works fine when i used it.

    0 讨论(0)
  • 2020-11-22 04:42

    One way is to add a unique number to the end of the url:

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

    Where you write uniqueId() to return something different each time it's called.

    0 讨论(0)
  • 2020-11-22 04:42

    This code may help you

    var sr = $("#Search Result");
    sr.load("AJAX-Search.aspx?q=" + $("#q")
    .val() + "&rnd=" + String((new Date).getTime())
    .replace(/\D/gi, ""));
    
    0 讨论(0)
  • 2020-11-22 04:45

    You can replace the jquery load function with a version that has cache set to false.

    (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);
    

    Place this somewhere global where it will run after jquery loads and you should be all set. Your existing load code will no longer be cached.

    0 讨论(0)
提交回复
热议问题