Skip to content Skip to sidebar Skip to footer

Ajax Request That Returns Json Array, Ie6/7 Is Caching It And Data Is Not Fresh

for some reason, IE6/7 is caching the ajax call that returns a json result set back. My page makes the call, and returns a json result which I then inject into the page. How can I

Solution 1:

You might want to add

Cache-Control:no-cache

to your HTML response headers when you're serving the JSON to tell the browser to not to cache the response.

In ASP.NET (or ASP.NET MVC) you can do it like this:

Response.Headers.Add("Cache-Control", "no-cache");

Solution 2:

you can change your settings in ie, but the problem most likely lies on your server. You can't go out and change all your users' browser settings. But if you want to at least check it on your browser, go to Internet Options->General (Tab)->Browsing History(section)->Settings (button)->"Every time I visit the webpage" Make sure you set it back, though, at some point.

To fix it on the server, have a look at http://www.mnot.net/cache_docs/

Using curl (w/ cygwin) for debugging is your great way to figure out what's actually being sent across the wire.

Solution 3:

If cache-control doesn't work for you (see DrJokepu's answer), according to the spec the content from any URL with a query string should be non-cacheable, so you might append a pointless query parameter to your request URL. The value doesn't matter, but if you really want to be thorough you can append the epoch value, e.g.:

var url = "myrealurl?x=" + (newDate()).getTime();

But this is a hack; really this should be solved with proper caching headers at the server end.

Solution 4:

In the controller action that returns a JsonResult, you need to specify in your headers to avoid caching:

ControllerContext.HttpContext.Response.AddHeader("Cache-Control", "no-cache");

Post a Comment for "Ajax Request That Returns Json Array, Ie6/7 Is Caching It And Data Is Not Fresh"