Creative Juices Bo. Co.

Satisfy Your Thirst For Something Refreshing!

Problem: IE8 Caching AJAX Results

Internet Explorer 8 Is Having Problems Caching Returned JSON Results

As you all know, I released CJ File Browser 3.1 the other day and I thought I had tested the heck out of it. Well, a user discovered that it wasn't working properly in IE8. The script relies heavily on jQueries getJSON function to communicate with the handler engine (The code that does the grunt work for CJ File Browser). The problem was occurring when the user uploaded or deleted the file. He had to close the browser window and the re-open it to see the results.

Immediately, I knew it was a caching issue. My first guess was the HTML file that handles the browser layout needed to have the no-caching META tags placed in the header. I added those, but the problem still persisted. Then it dawned on me, I had a similar problem with Safari and a problem it was having with caching iFrame content. This was solved by adding a simple URL parameter with it's value set to the current time. Safari would then see that something was different and would no longer cache the page.

The Solution

To solve this problem with jQuery, you need to add a parameter to the call and set it's value to the current time each time it's sent. My solution looks like this:

var nocache = new Date().getTime();
jQuery.getJSON("myFunction.cfm", { "nocache": nocache }, function (result, status) {
   // ... handle the results
});

This solved the issue and it was no longer caching in IE8. Pretty simple solution really. Not sure why it's caching those results, but hopefully this prevents headaches for you!