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!