79

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.

Barka
  • 8,764
  • 15
  • 64
  • 91
  • 1
    Show the full `get()` example? – Jivings Jan 12 '12 at 20:02
  • I like to pass the date/time of a file with the page (assuming I'm using a server-side scripting language), and then pass that with the request for the page as a querystring parameter. The reason I do this is because it allows caching, unless the page has actually changed. – Reinstate Monica Cellio Jan 12 '12 at 20:07
  • 1
    Using cache:false appends a timestamp to the ajax/json request e.g. {"get":"modified"}&_=1394836303603 which broke my API requests. It took way too many hours to figure out what was adding the timestamp, as it is buried un the jQuery docs. Instead of using cache:false, just add your own timestamp, assuming your API will not care if you add an unknown parameter. Like this: {"get":"modified", "timestamp":"1394836303603"} This also lets you have finer control over what items are cached, and which are not. – Jim Bergman Mar 15 '14 at 01:18
  • Why you do not want to use $.ajaxSetup? It is a straight forward and gets the job done. – usefulBee Apr 04 '14 at 16:33

7 Answers7

116

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);
});
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • 6
    thanks $.now() is the shorthand for new Date().getTime(). http://api.jquery.com/jQuery.now/ – Barka Jan 12 '12 at 21:36
  • 2
    You can now also do `Date.now()`. – Jivings Aug 07 '14 at 08:14
  • `Date.now()` was only standardized in ECMA-262 5th edition. To polyfill for older engines, you may use: `(Date.now ? Date.now() : (new Date().valueOf()))` via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now – Steven Jan 13 '16 at 19:02
  • 2
    or, since we have jquery, just use what we've got. :) – Kevin B Jan 13 '16 at 19:04
  • 1
    This should be the accepted answer... since the OP was asking specifically about `$.get();` – Serj Sagan Jan 22 '16 at 10:47
  • 1
    @SerjSagan i disagree with that, since prior to jQuery 3.0 the better option would be to use $.ajax. in 3.0, $.get can accept an options object too. :) – Kevin B Jan 26 '16 at 23:32
64

I think you have to use the AJAX method instead which allows you to turn caching off:

$.ajax({
  url: "test.html",
  data: 'foo',
  success: function(){
    alert('bar');
  },
  cache: false
});
Jivings
  • 22,834
  • 6
  • 60
  • 101
37

To me, the correct way of doing it would be the ones listed. Either ajax or ajaxSetup. If you really want to use get and not use ajaxSetup then you could create your own parameter and give it the value of the the current date/time.

I would however question your motives in not using one of the other methods.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • 1
    Worth noting that in my particular app, I use cache: false in ajaxSetup (the majority of my calls are to json endpoints for payloads) and then I use $.ajax(..., cache: true) for any specific templates or things that I'll want cached. I prefer this anyway, as it makes things pretty explicit, and I'm calling more of the payload calls than static data. – David Feb 16 '13 at 19:23
7

Per the JQuery documentation, .get() only takes the url, data (content), dataType, and success callback as its parameters. What you're really looking to do here is modify the jqXHR object before it gets sent. With .ajax(), this is done with the beforeSend() method. But since .get() is a shortcut, it doesn't allow it.

It should be relatively easy to switch your .ajax() calls to .get() calls though. After all, .get() is just a subset of .ajax(), so you can probably use all the default values for .ajax() (except, of course, for beforeSend()).

Edit:

::Looks at Jivings' answer::

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

Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
5

Set cache: false in jQuery.get call using Below Method

use new Date().getTime(), which will avoid collisions unless you have multiple requests happening within the same millisecond.

Or

The following will prevent all future AJAX requests from being cached, regardless of which jQuery method you use ($.get, $.ajax, etc.)

$.ajaxSetup({ cache: false });
Sangeet Shah
  • 3,079
  • 2
  • 22
  • 25
3

Note that callback syntax is deprecated:

Deprecation Notice

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

Here a modernized solution using the promise interface

$.ajax({url: "...", cache: false}).done(function( data ) {
    // data contains result
}).fail(function(err){
    // error
});
mar10
  • 14,320
  • 5
  • 39
  • 64
  • It's 2023 and jQuery has zero plans to depreciate this in official builds. If you really want to be ahead of the game, remove jQuery entirely and use pure JS with fetch/async/await methods. – WiiLF Jan 03 '23 at 05:03
2

I'm very late in the game, but this might help others. I hit this same problem with $.get and I didn't want to blindly turn off caching and I didn't like the timestamp patch. So after a little research I found that you can simply use $.post instead of $.get which does NOT use caching. Simple as that. :)

RitchieD
  • 1,831
  • 22
  • 21
  • 1
    I'm late too :) thanks for your contribution, it is working fine – Angel M. Sep 03 '15 at 09:23
  • 3
    This is a bad advice as GET and POST are different verbs and should be used for different things. The same server url may be filtering by verb so for example a POST won't work if the server only expects a GET. Or even worse, the server may have different behaviors for GET and POST (which is very common actually). – Andrew Jan 07 '18 at 08:53
  • @Andrew: If not jquery ajax always had the flaw to opt for get in the first place while that is _not_ post and _not_ the correct verb for ajax. So post is fine. and _correct_. – hakre Jul 04 '21 at 07:07
  • I'm not sure if I understand what you mean. If you use a `get` instead of a `post`, that's wrong too. You should [use `get` or `post`](https://www.w3schools.com/tags/ref_httpmethods.asp) depending on what the endpoint does, and not use `post` to solve caching issues with `get`. – Andrew Jul 05 '21 at 00:02