How to set cache: false in jQuery.get call

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

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

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

I a. looking for a fourth method different from the three methods that have been identified so far:

  • Running this process globally through ajaxSetup

  • Using .ajax call instead of .get call

  • By doing it manually, adding a new parameter containing a timestamp to your .get call.

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

+62
jquery caching
Jan 12 2018-12-12T00:
source share
7 answers

It would be right for me to do this. Either ajax or ajaxSetup . If you really want to use get and not use ajaxSetup , then you can create your own parameter and assign it the value of the current date / time.

I would, however, question your motives without using one of the other methods.

+34
Jan 12 2018-12-12T00:
source share

Add the parameter yourself.

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



As in jQuery 3.0, you can do the following:

 $.get({ url: url, cache: false }).then(function(rdata){ console.log(rdata); }); 
+77
Jan 12 2018-12-12T00:
source share

I think you should use the AJAX method instead, which allows you to disable caching:

 $.ajax({ url: "test.html", data: 'foo', success: function(){ alert('bar'); }, cache: false }); 
+62
Jan 12 2018-12-12T00:
source share

In the JQuery documentation .get() uses only url , data (content), dataType and success dataType as its parameters. What you really want to do is modify the jqXHR object before sending it. Using .ajax() this is done using the beforeSend() method. But since .get() is a shortcut, it does not allow it.

It is relatively easy to switch .ajax() calls to .get() calls. In the end, .get() is just a subset of .ajax() , so you can use all the defaults for .ajax() (except, of course, beforeSend() ).

Edit:

:: Take a look at Jivings answer ::

Oh yes, I forgot about the cache parameter! While beforeSend() is useful for adding other headers, the built-in cache parameter is much simpler here.

+7
Jan 12 2018-12-12T00:
source share

I'm very late in the game, but it can help others. I ran into this problem with $ .get and I did not want to blindly disable caching and I did not like the timestamp patch. So after a little research, I found that you can just use $ .post instead of $ .get, which does NOT use caching. Just like that. :)

+2
Jul 22 '15 at 13:10
source share

Set cache: false in jQuery.get call using method below

use the new Date().getTime(), which will avoid collisions if you don't have multiple requests within one millisecond.

Or

The following will prevent caching of all future AJAX requests, no matter which jQuery method you use ($ .get, $ .ajax, etc.)

 $.ajaxSetup({ cache: false }); 
+2
Oct. 20 '15 at 10:06
source share

Note that the callback syntax is deprecated :

Refusal Notice

The jqXHR.success (), jqXHR.error (), and jqXHR.complete () callback methods introduced in jQuery 1.5 are deprecated from jQuery 1.8. To prepare the code for their possible removal, use jqXHR.done (), jqXHR.fail () and jqXHR.always () instead.

Here's a streamlined solution using the promise interface

 $.ajax({url: "...", cache: false}).done(function( data ) { // data contains result }).fail(function(err){ // error }); 
+1
Jan 07 '16 at 9:27
source share



All Articles