Skip to main content

Unexpected Caching of AJAX results in IE8


I'm having a serious issue with Internet Explorer caching results from a JQuery Ajax request.



I have header on my web page that gets updated every time a user navigates to a new page. Once the page is loaded I do this




$.get("/game/getpuzzleinfo", null, function(data, status) {
var content = "<h1>Wikipedia Maze</h1>";
content += "<p class='endtopic'>Looking for <span><a title='Opens the topic you are looking for in a separate tab or window' href='" + data.EndTopicUrl + "' target='_blank'>" + data.EndTopic + "<a/></span></p>";
content += "<p class='step'>Step <span>" + data.StepCount + "</span></p>";
content += "<p class='level'>Level <span>" + data.PuzzleLevel.toString() + "</span></p>";
content += "<p class='startover'><a href='/game/start/" + data.PuzzleId.toString() + "'>Start Over</a></p>";

$("#wikiheader").append(content);

}, "json");



It just injects header info into the page. You can check it out by going to www.wikipediamaze.com and then logging in and starting a new puzzle.



In every browser I've tested (Google Chrome, Firefox, Safari, Internet Explorer) it works great except in IE. Eveything gets injected just fine in IE the first time but after that it never even makes the call to /game/getpuzzleinfo . It's like it has cached the results or something.



If I change the call to $.post("/game/getpuzzleinfo", ... IE picks it up just fine. But then Firefox quits working.



Can someone please shed some light on this as to why IE is caching my $.get ajax calls?



UPDATE



Per the suggestion below, I've changed my ajax request to this, which fixed my problem:




$.ajax({
type: "GET",
url: "/game/getpuzzleinfo",
dataType: "json",
cache: false,
success: function(data) { ... }
});


Source: Tips4allCCNA FINAL EXAM

Comments

  1. IE is notorious for its aggressive caching of Ajax responses. As you're using jQuery, you can set a global option:

    $.ajaxSetup({
    cache: false
    });


    which will cause jQuery to add a random value to the request query string, thereby preventing IE from caching the response.

    Note that if you have other Ajax calls going on where you do want caching, this will disable it for those too. In that case, switch to using the $.ajax() method and enable that option explicitly for the necessary requests.

    See http://docs.jquery.com/Ajax/jQuery.ajaxSetup for more info.

    ReplyDelete
  2. As marr75 mentioned, GET's are cached.

    There are a couple of ways to combat this. Aside from modifying the response header, you can also append a randomly generated query string variable to the end of the targeted URL. This way, IE will think it is a different URL each time it is requested.

    There are multiple ways to do this (such as using Math.random(), a variation on the date, etc).

    Here's one way you can do it:

    var oDate = new Date();
    var sURL = "/game/getpuzzleinfo?randomSeed=" + oDate.getMilliseconds();
    $.get(sURL, null, function(data, status) {
    // your work
    });

    ReplyDelete
  3. Gets are always cacheable. One strategy that may work is to edit the response header and tell the client to not cache the information or to expire the cache very soon.

    ReplyDelete
  4. this is what i do for ajax calls:

    var url = "/mypage.aspx";
    // my other vars i want to add go here
    url = url + "&sid=" + Math.random();
    // make ajax call


    it works pretty well for me.

    ReplyDelete
  5. If you are calling ashx page you can also disable caching on the server with the following code:

    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

    ReplyDelete
  6. The answers here are very helpful for those who use jQuery or for some reason directly use the xmlHttpRequest object...

    If you're using the auto-generated Microsoft service proxy its not as simple to solve.

    The trick is to use Sys.Net.WebRequestManager.add_invokingRequest method in the event handler change the request url:

    networkRequestEventArgs._webRequest._url = networkRequestEventArgs._webRequest._url + '&nocache=' + new Date().getMilliseconds();


    I've blogged about this: http://yoavniran.wordpress.com/2010/04/27/ie-caching-ajax-results-how-to-fix/

    ReplyDelete
  7. Just wrote a blog on this exact issue only using ExtJS (http://thecodeabode.blogspot.com/2010/10/cache-busting-ajax-requests-in-ie.html
    )

    The problem was as I was using a specific url rewriting format I couldn't use conventional query string params (?param=value), so I had write the cache busting parameter as a posted variable instead..... I would have thought that using POST variables are a bit safer that GET, simply because a lot of MVC frameworks use the pattern

    protocol://host/controller/action/param1/param2

    and so the mapping of variable name to value is lost, and params are simply stacked... so when using a GET cache buster parameter

    i.e. protocol://host/controller/action/param1/param2/no_cache122300201

    no_cache122300201 can be mistaken for a $param3 parameter which could have a default value

    i.e.

    public function action($param1, $param2, $param3 = "default value")
    {
    //..//
    }

    no chance of that happening with POSTED cache busters

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex