I don’t really know if I should call this as a bug. There are two different schools of thought on this issue. One school of thought says, “You gotta do what all the other browsers are doing.” while the other says, “HTTP specification states that GET requests can be cached”
This puts us developers in a perplexing situation. What am I talking about? Let’s revisit the scenario.
So I was making a getJSON jQuery request to load all of the states a user is interested in from the DB.
The following was the original call:
$.getJSON('<%= Url.Action("GetSelectedStates", "Profile") %>', function(data) {
for (var i = 0; i < data.length; i++) {
$("#stateSelection").trigger("addItem", data[i]);
}
});
The user can then view the state information in a FCBK type control.

Now if the user deletes a state or adds one, the db will get updated on the form post event. Next time around the same getJSON call will be executed and the new states will be displayed.
Sounds easy right? Well it isn’t. Turns out, this works on every browser but IE. IE for some reason caches your JSON request. This means the JSON call is completely bypassed the second time around/and any subsequent times and your updates are never displayed again.
There are a couple of ways you can solve this issue:
1. Use a POST and add an additional parameter of “json”. However, I don’t quite like this approach.
2. FAKE the JSON request to be unique by adding a random parameter in the JSON call. This can be done rather easily. I can now do a DateTime.Now in the JSON call and then just accept that parameter from my controller. This DateTime.Now will always be different and thus will prevent your JSON calls from being cached.
Here is the modified getJSON method:
function loadStateData() {
$.getJSON('<%= Url.Action("GetSelectedStates", "Profile") %>',
{ currentTime : '<%= DateTime.Now %>' },
function(data) {
for (var i = 0; i < data.length; i++) {
$("#stateSelection").trigger("addItem", data[i]);
}
});
}
And here is my controller code:
public JsonResult GetSelectedStates(DateTime currentTime)
{
JsonResult result = new JsonResult();
Guid userId = sessionService.GetLoggedInUserId();
List stateSelections = physicianService.GetPhysicianStateSelections(userId);
var data = (from s in stateSelections
select new
{
title = CacheServiceInstance.GetStateProvinceById(s.StateProvinceId).Name.Trim(),
value = s.StateProvinceId
}).ToList();
result = Json(data);
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
}
This should take care of the issue! Enjoy!




Facebook style AutoComplete using ASP.NET MVC and FCBKComplete « .NET Blog
[...] browser make a brand new request. To read more about the Controller action implementation, see this previous post of [...]
Alex M.
This is awesome stuff! Helped me out a lot. Thanks!
jqModal with ASP.NET MVC « .NET Blog
[...] cache the request and bypass the GET calls. To read more about this, please read my post on IE and the JSON Caching Issue 2. I am calling hash.w.fadeOut. What is that? hash.w is nothing but a way to access the jqmodal [...]
Steve
You can also disable caching on the MVC function like this..
[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult NonCacheableData()
{
return View();
}
Robert MacLean
The other option is to use POST rather than GET.
Anup
Hello, from a purist point of view, POST should only be used when POSTING data and GET should be used for all GET related operations. Not that using a POST would hurt, but I think in general we should all work towards using the HTTP protocol as it is intended to be used.
I have actually created a bunch of JS utility classes that make the process of stuffing a random # even more easy. Will be blogging about that soon! Thank you for your comments.