Weird $.getJSON caching behaviour in IE6 and IE7
So I’ve been doing some more Ajax stuff using JQuery during my ASP.Net maintenance work. One of the joys of maintenance is introducing new features into an existing application from time to time. It allows one an opportunity to try out some new stuff occasionally. I decided to use the hip new JSON as the data-interchange format for my client-server communications using JQuery’s $.getJSON function for sending back requests to the ASP.Net server. It’s pretty easy to use, all the house-keeping is done by JQuery.
$.getJSON("MyPage.aspx",
{ Param1: "Param1Value" }
function(result) {
//manipulate result data here
alert(result.value);
});
Just send the parameters and get the results. Of course you’ll need to serialize the results into the JSON format using the ASP.Net Ajax JavaScript Serializer. That’s what I do anyway, just serialize the object I want to send back and write all of it on the Response using Response.Write(). And then do all the processing at the client. JSON is used purely as the means of sending back and forth between the browser and the server. It was working fine until I discovered some weird random behaviour in IE6 and IE7 . The browser seemed to be caching the Ajax requests if all the parameters were the same as the last request. Instead of contacting the server for the latest data the browser returned cached results. It was happening randomly in IE6 and 7. I had to whip up Fiddler to get to the ‘root’ of the problem. Anyway turns out there’s a simple fix. Adding $.ajaxSetup({ cache: false }); before the request solved the problem. Basically JQuery now forces the browser not to use the cached results by adding a new randomly generated number as a parameter to every request. No more caching means new results every time…
Thanks for your time on this. You saved me, I was head scratching on this one. Nice clean site my complements.
bill Soetebier
July 22, 2009 at 6:35 pm