Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent .NET JSON feeds from being cached

I have created simple .aspx page which queries a database for some live data and then returns a JSONP (or at last a JSONP-like) feed of Content-Type application/json; charset=utf-8

Here is the format of the output, more or less:

jsonp1307643489579([
  ["12345","Text Here","99999","More Text Here","True","False","7/31","1"...],
  ["12345","Text Here","99999","More Text There",...]
]

Then comes a JQuery .getJSON call:

var url = "myURL.aspx?id=123&callBack=?";
$.getJSON(url, null, function(msg) { etc etc.

It all works fine except for the following. In my development environment and on my local server, fresh data come back live every time. But on the production web server, the data stubbornly caches until I recycle the IIS application pool (!)

Some things I have tried without success.

1/ cache: false in ajaxSetup didn't work.

2/ Turned off output caching in the web.config.

2a/ OutputCache Location="None" in the aspx page declarations doesn't do it.

3/ Added random unique querystring data to the .getJSON(url) call. Seeing as how we are appending a unique callback param to each call, I guess this was already happening anyway.

Any idea why my web server is holding on to these cached application/JSON files?

EDIT: i am viewing the actual .aspx feeds as they come down from the web server, and they are cached there. So to my understanding, it's a web server caching issue and not necessarily a JQUERY caching issue.

like image 730
LesterDove Avatar asked Jun 09 '11 18:06

LesterDove


People also ask

Can JSON be cached?

In order to add data in cache as JSON, the objects need to be created according to the JSON standards provided by NCache. JsonObject is added in the cache against a unique key. This key will be used to perform further operations on the cache.

Do browsers cache JSON responses?

Request Headers 2nd request: Thx for response. Yes you can cache JSON responses.


3 Answers

Before you make any getJSON calls, use this:

jQuery.ajaxSetup({
   cache: false
});

I battled this same issue for about a half hour yesterday. I'm not sure why manually adding a random query string wouldn't work, but this solved the issue for me. Cache hits were completely random until I added the general setup above.

like image 182
Stefan Kendall Avatar answered Oct 09 '22 13:10

Stefan Kendall


To elaborate on @Stefan's answer, setting cache to false tells jquery to append a random query string to make the request unique.

Example: myURL.aspx?id=123&callBack=&_=13245897565132154878

like image 29
kroehre Avatar answered Oct 09 '22 13:10

kroehre


It turned out that it was indeed the .ASPX server page caching the data. For all of the focus on the client-side AJAX stuff, I overlooked the obvious.

So I added a litany of preventative measures on the server side (Response.ExpiresAbsolute, et cetera) and it did the job.

like image 25
LesterDove Avatar answered Oct 09 '22 12:10

LesterDove