Reputation: 5976
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?
Upvotes: 3
Views: 1818
Reputation: 24212
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.
Upvotes: 1