Posts
60
Comments
36
Trackbacks
18
JQuery $.getJSON Internet Explorer (IE) cache ASP.NET MVC

So I was getting a little frustrated when trying to find an answer (client-side) to the issue that I had on a new tutorial I am writing.  Usually if I find something tedious, but easy, I try to post a short write up.  I was creating a Math application for my daughter, that presents her with 2 single digit numbers to add, once entered, you push enter and if right, the application dynamically gathers a new random question from the server.  On IE, this was fairly straightforward.  Here is my Javascript, using JQuery and ASP.NET MVC to respond with a C# Object for parsing in the browser.

      function getProblem() {
          // Here is the invoke         // This is the method to pass it back to.
          var d = new Date();
          $.getJSON("MathPractice/Get?" + d.getMilliseconds(), showProblem);
      }

Notice that the normal method should just be $.getJSON("MathPractice/Get", responseMethodName);.  This was working flawlessly in Mozilla, but noticed that in IE it never had new values.  IE looks in the cache first to get the answer to this question, and oops that doesn't work.  So what I did to alleviate is I added the Date() to it as a query string to make sure that it isn't going to cache the result.  There is another way to do it via ASP.NET MVC, however this is the easiest fix.

posted on Friday, March 13, 2009 12:25 PM Print
Comments
Gravatar
# re: JQuery $.getJSON Internet Explorer (IE) cache ASP.NET MVC
Eric
3/16/2009 2:48 PM
  
There is also a ajaxSetup method that enables/disables caching:
function getProblem() {
// Here is the invoke // This is the method to pass it back to.
$.ajaxSetup({ cache: false });
$.getJSON("MathPractice/Get?" + d.getMilliseconds(), showProblem);
}

Gravatar
# re: JQuery $.getJSON Internet Explorer (IE) cache ASP.NET MVC
jancel
3/31/2009 10:29 AM
  
That looks like a much more solvable solution. Thanks.
Gravatar
# re: JQuery $.getJSON Internet Explorer (IE) cache ASP.NET MVC
Beyers
4/28/2009 10:00 AM
  
Excellent, was also struggling with this issue, and the ajaxSetup call is definately the neetest solution.
Gravatar
# re: JQuery $.getJSON Internet Explorer (IE) cache ASP.NET MVC
Kevin Lee
5/19/2009 3:40 PM
  
Thanks a ton!! I was struggling with this issue and couldn't quite place my finger on it. I ended up passing the Date in the data rather that the query string. It looked something like this:
$.getJSON("MathPractice/Get, { TimeStamp: d.miliseconds }, showProblem);

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 2 and 6 and type the answer here:
News
My Developer Notebook! This also happens to be my opinion place. Thanks for coming by.