I\'m having a serious issue with Internet Explorer caching results from a JQuery Ajax request.
I have header on my web page that gets updated every time a user navig
Gets are always cacheable. One strategy that may work is to edit the response header and tell the client to not cache the information or to expire the cache very soon.
If you are using ASP.NET MVC, it is enough to add this line on top of the controller action:
[OutputCache(NoStore=true, Duration = 0, VaryByParam = "None")]
public ActionResult getSomething()
{
}
IE is within its rights to do this caching; to ensure the item isn't cached, the headers should be set accordingly.
If you are using ASP.NET MVC, you can write an ActionFilter
; in OnResultExecuted
, check filterContext.HttpContext.Request.IsAjaxRequest()
. If so, set the response's expire header: filterContext.HttpContext.Response.Expires = -1;
As per http://www.dashbay.com/2011/05/internet-explorer-caches-ajax/:
Some people prefer to use the Cache - Control: no - cache header instead of expires. Here’s the difference:
Cache-Control:no-cache – absolutely NO caching
Expires:-1 – the browser “usually” contacts the Web server for updates to that page via a conditional If-Modified-Since request. However, the page remains in the disk cache and is used in appropriate situations without contacting the remote Web server, such as when the BACK and FORWARD buttons are used to access the navigation history or when the browser is in offline mode.
IE is notorious for its aggressive caching of Ajax responses. As you're using jQuery, you can set a global option:
$.ajaxSetup({
cache: false
});
which will cause jQuery to add a random value to the request query string, thereby preventing IE from caching the response.
Note that if you have other Ajax calls going on where you do want caching, this will disable it for those too. In that case, switch to using the $.ajax() method and enable that option explicitly for the necessary requests.
See http://docs.jquery.com/Ajax/jQuery.ajaxSetup for more info.