开发者

How to prevent .NET JSON feeds from being cached

开发者 https://www.devze.com 2023-03-12 07:25 出处:网络
I have created simple .aspx page which queries a database for some live data an开发者_C百科d then returns a JSONP (or at last a JSONP-like) feed of Content-Type application/json; charset=utf-8

I have created simple .aspx page which queries a database for some live data an开发者_C百科d 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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消