JQuery ajax downloader and browser caching

I am using jQuery to dynamically load data through ajax. Whenever the user initiates an ajax request (i.e.. Presses a button or loads a page, etc.), I show the ajax gj loader. After completing ajax requests (or errors), I hide the bootloader image.

This works great most of the time. But I noticed (e.g. in IE7) that ajax calls that are sent to load the page get cached (I think). That way, basically, when I refresh the page and apparently the ajax call starts up, IE loads the cached data. As a result, a successful (or erroneous) callback in the jQuery .ajax() function is never called, and the loader image remains undefined. How can this be done?

+4
source share
3 answers

If you want to disable ajax result caching on your entire site, use . ajaxSetup () :

 $.ajaxSetup( { cache : false } ); 

Then you can override the behavior in each case with:

 $.ajax ({ ... cache: true, ... 
+3
source

.ajax() offers a cache property, which is true by default:

 $.ajax({ ... cache: false }); 

http://api.jquery.com/jQuery.ajax/

Alternatively, you can add a random number to the query string of the query.

 $.ajax({ url: '/path/script.pl', dataType: 'text', type: 'GET', data: { q: ~~(Math.random()*200) }, success: function(data){ // data } }); 
+2
source

jQuery.ajax really has a built-in option to avoid this:

 $.ajax( { cache : false } ); 

The documentation is included in the ajax method: jQuery.ajax ()

From the documentation:

  cache
 Default: true, false for dataType 'script' and 'jsonp'
 If set to false it will force the pages that you request to not be cached by the browser.
0
source

All Articles