I am sure everyone else who lives in the world of jQuery knows this, so this is more here for my own future reference and avoiding and more wasting of time, but if you are going to us $.getJSON helper in jQuery, append a time stamp to it to prevent odd caching behavior in IE. See the example below.


$.getJSON("/story/story_id.php?_="+(new Date().getTime()), function(json) {
$('#next_story_id').html('S' + json.next_story_id);
});

This might not be the best way to do it and really, this is good practice for all dynamic requests to avoid caching issues from the browser and the server. Chalk this up as a good learning experience that sucked 30 minutes of my life. I hate front end stuff.

5 Comments

  1. Aaron says:

    In this situation you’re better off using the $.ajax helper instead of $.getJSON :

    $.ajax({
    url: “/story/story_id.php”,
    type: “GET”,
    cache: false,
    dataType: “json”
    success: function(json){
    $(‘#next_story_id’).html(‘S’ + json.next_story_id);
    }
    });

    “cache” is the key and jquery will automatically append a timestamp.

  2. bjorn says:

    $.ajaxSetup({ cache: false }); might be a better solution

  3. Wayne says:

    Thanks, will do that instead. I am new to this whole jQuery thing, so I am learning as I go.

  4. Mai says:

    Thanks bjorn! It’s working :)

  5. daoming says:

    I had the same problem.

    Here is the solution if you are using MVC:

    [OutputCache(Duration = 0, VaryByParam = "None")]

    http://daomingworks.wordpress.com/2009/12/16/jquery-getjson-data-has-been-cached/

Leave a Reply

You must be logged in to post a comment.