views:

13169

answers:

7

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

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

But the returned result is not always reflected. For example, I made a change in the response that spit out a stack trace but the stack trace did not appear when I clicked on the search button. I looked at the underlying PHP code that controls the ajax response and it had the correct code and visiting the page directly showed the correct result but the output returned by .load was old.

If I close the browser and reopen it it works once and then starts to return the stale information. Can I control this by jQuery or do I need to have my PHP script output headers to control caching?

+3  A: 

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.

Lou Franco
+1  A: 

This is of particular annoyance in IE. Basically you have to send 'no-cache' HTTP headers back with your response from the server.

Xian
+39  A: 

You have to use a more complex function like $.ajax() if you want to control caching on a per-request basis. Or, if you just want to turn it off for everything, put this at the top of your script:

$.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
});
John Millikin
thank you! Is this even located in the jQuery documentation? Ahh... found it. Kind of berried. This had been kicking by butt for most of the day... Thanks again!
J.13.L
All this cache: false does is append a number (I believe its a timestamp) to the end of a url when making the request. The other place to handle the cache settings are from the server or web app by setting various HTTP response headers, like Expires, Pragma, etc...
Redbeard 0x0A
This piece of information hepled me greatly - it helped me locate a really nagging bug
Luke101
Superb thanks!!
Chris
A: 

For PHP, add this line to your script which serves the information you want:

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

or, add a unique variable to the query string:

"/portal/?f=searchBilling&x=" + (new Date()).getTime()
nickf
The cache: false option on the $.ajax function automatically adds the unique variable to the query string like your second suggestion.
Redbeard 0x0A
A: 

Hi,

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.

stringlist.com
+4  A: 

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").val(data);
    }
});
Marshall
A: 
/**
 * 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;
}
Sasha