Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cache: false in jQuery.get call

Tags:

jquery

caching

jQuery.get() is a shorthand for jQuery.ajax() with a get call. But when I set cache:false in the data of the .get() call, what is sent to the server is a parameter called cache with a value of false. While my intention is to send a timestamp with the data to the server to prevent caching which is what happens if I use cache: false in jQuery.ajax data. How do I accomplish this without rewriting my jQuery.get calls to jQuery.ajax calls or using

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

update: Thanks everyone for the answers. You are all correct. However, I was hoping that there was a way to let the get call know that you do not want to cache, or send that value to the underlying .ajax() so it would know what to do with it.

I a. looking for a fourth way other than the three ways that have been identified so far:

  1. Doing it globally via ajaxSetup

  2. Using a .ajax call instead of a .get call

  3. Doing it manually by adding a new parameter holding a timestamp to your .get call.

I just thought that this capability should be built into the .get call.

like image 845
Barka Avatar asked Jan 12 '12 19:01

Barka


People also ask

How do I set cache false in Ajax call?

The cache: false is used by developers to prevent all future AJAX requests from being cached, regardless of which jQuery method they use. We can use $. ajaxSetup({cache:false}); to apply the technique for all AJAX functions.

Why we write cache false in Ajax?

cache (default: true, false for dataType 'script' and 'jsonp') Type: Boolean If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters.

Do Ajax requests get cached?

Fact #1 : Ajax Caching Is The Same As HTTP Caching It simply obeys the normal HTTP caching rules based on the response headers returned from the server.

What is cache true in Ajax?

cache:true is the default and does not always get the content from the cache. The cache-ability of an item on the browser is determined by: The response headers returned from the origin web server.


1 Answers

Add the parameter yourself.

$.get(url,{ "_": $.now() }, function(rdata){   console.log(rdata); }); 

As of jQuery 3.0, you can now do this:

$.get({   url: url,    cache: false }).then(function(rdata){   console.log(rdata); }); 
like image 166
Kevin B Avatar answered Sep 26 '22 01:09

Kevin B