2

I have a situation in which a dropdown on my asp.net MVC page is being populated by an AJAX GET request for json data. The first request returns the data with a 200 OK response. Subsequent calls return a 304 Not Modified request, and the dropdown is still populated correctly. This is all good and well.

However, in another part of my application, another user may modify the content of the repository from which the data for the dropdown is being returned. What I want is that, after such a modification, any subsequent request by other users for the dropdown's data should NOT return a 304 Not Modified result but do a refetch of the data.

How do I achieve this?

user247702
  • 23,641
  • 15
  • 110
  • 157
Shawn de Wet
  • 5,642
  • 6
  • 57
  • 88
  • 1
    Is my answer missing anything you would like to know? – user247702 Dec 04 '13 at 16:07
  • Well your answer doesn't answer my question. I actually like that fact that the response is being cached. But I need to know how to clear this cache. In fact your statement "You could do smart caching and check to see if the data has been modified, but this would be non-trivial (at least I think so, I've never tried it)." alludes to what I want to know. – Shawn de Wet Dec 05 '13 at 03:56
  • 1
    That's too broad for an answer on SO I think. Perhaps if you try to implement such a feature and if run into a certain problem you can ask for help around here, but asking for a complete start-to-end guide is off-topic for here. – user247702 Dec 05 '13 at 08:38

1 Answers1

1

I always completely disable AJAX caching. Internet Explorer in particular can do agressive caching on AJAX.

You could do smart caching and check to see if the data has been modified, but this would be non-trivial (at least I think so, I've never tried it).

Global.asax.cs

protected void Application_BeginRequest()
{
    this.DisableAJAXCaching();
}

Extensions.cs

public static void DisableAJAXCaching(this HttpApplication application)
{
    /*
     * "CACHE ALL THE AJAX REQUESTS"
     *  - Internet Explorer
     */
    if (application.Request.Headers["X-Requested-With"] != null)
    {
        application.Response.AppendHeader("Expires", "0");
    }
}

The X-Requested-With indicates an AJAX request. Your JS framework of choice should normally support it.

user247702
  • 23,641
  • 15
  • 110
  • 157