102

I'm playing around with ASP.net MVC and JQuery at the moment. I've come across behavour which doesn't seem to make sense.

I'm calling JQuery's $.getJSON function to populate some div's. The event is triggered on the $(document).ready event. This works perfectly.

There is a small AJAX.BeginForm which adds another value to be used when populating the divs. It calls the remote function correctly and upon success calls the original javascript function to repopulate the divs.

Here is the weird part: In FireFox and Chrome - Everything works. BUT In IE8 (Beta) this second call to the populate Div script (which calls the $.getJSON function) gets cached data and does not ask the server!

Hope this question makes sense: In a nut shell - Why is $.getJSON getting cached data? And why is it only effecting IE8?

RAM
  • 2,413
  • 1
  • 21
  • 33
Andrew Harry
  • 13,773
  • 18
  • 67
  • 102

7 Answers7

107

This is how it worked for me...

$.ajaxSetup({ cache: false });
$.getJSON("/MyQueryUrl",function(data,item) {
     // do stuff with callback data
     $.ajaxSetup({ cache: true });
   });
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
Jitesh Patil
  • 1,334
  • 1
  • 8
  • 13
  • I was struggling with this problem and your solution gave a quick way to solve it. :) I have a question, do you know what options can be used for $.ajaxSetup? jQuery documentation does not provide details unfortunately... – Achimnol Jul 23 '09 at 07:09
  • 1
    The available options are identical to $.ajax See http://docs.jquery.com/Ajax/jQuery.ajax#options for more information – Dan Esparza Aug 11 '09 at 21:37
  • 12
    A little warning for the code above - it contains race condition. Because the call and its response are asynchronous, you should call `$.ajaxSetup({ cache: true });` right after the `getJSON()` and not in the callback. – sax Mar 10 '12 at 17:36
67

Just to let you know, Firefox and Chrome consider all Ajax request as non-cachable. IE (all versions) treat Ajax call just as other web request. That's why you see this behavior.
How to force IE to download data at each request:

  • As you said, use 'cache' or 'nocache' option in JQuery
  • Add a random parameter to the request (ugly, but works :))
  • On server side, set cachability (for example using an attribute, see below)

Code:

public class NoCacheAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
}
Nico
  • 13,320
  • 6
  • 32
  • 33
16

Thanks Kent for your answer. Using $.ajax('{ cache: no }'); worked perfectly. [edit]

Or at least I thought i did. Seems that the jquery $.getJSON isn't reading any changes made to the $.ajax object.

The solution that ended up working was to add a new parameter manually

var noCache = Date();
$.getJSON("/somepage/someaction", { "noCache": noCache }, Callback);

the date resolution is only to the minute; which effectively means this solution still caches for upto one minute. This is acceptable for my purposes.

Andrew Harry
  • 13,773
  • 18
  • 67
  • 102
11

I solved this same problem by placing the following attribute on the Action in the Controller:

[OutputCache(Duration = 0, VaryByParam = "None")]
Guy
  • 65,082
  • 97
  • 254
  • 325
4

If you're using ASP.net MVC, consider adding an extension method to easily implement no caching like so:

    public static void NoCache(this HttpResponse Response)
    {
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetValidUntilExpires(false);

        Response.Expires = -1;
        Response.ExpiresAbsolute = DateTime.MinValue;
        Response.AddHeader("Cache-Control", "no-cache");
        Response.AddHeader("Pragma", "no-cache");
    }
Josh
  • 5,281
  • 3
  • 19
  • 16
  • Nice idea - so you call this extension method on the server during the callback right? – Guy Aug 12 '09 at 21:12
2

Ready for THE answer ?

http://lestopher.tumblr.com/post/21742012438/if-youre-using-ie8-and-getjson

So, just add

jQuery.support.cors = true;  

at the beginning of your script and BANG it works !

stadja
  • 56
  • 6
2

You may need to send a cache-breaker.

I would recommend using $.ajax( { cache: no }) just in case ( adds a random suffix to the get request)

( I tend to use $.ajax everywhere these days, more tuneable )

Kent Fredric
  • 56,416
  • 14
  • 107
  • 150