views:

811

answers:

3

I am using jQuery to get back some JSON data from the server. I am using a POST verb, but even after setting the WebMethod CacheDuration attribute, the JSON doesn't get cached.

I checked the response headers with firebug and the Cache-Control is still set to no-cache. How can i cache these request on the client and avoid the server to be hit every time.

UPDATE

After reading this post from scottGu I thought it would have been safe to go on to use a POST request. Does his post not apply to the kind of operation i would be trying to do ? (getting data from the server without modifying it). In fact after changing the verb to GET, i am not even getting to the web service ...

+2  A: 

You should be using a get request. Post does not cache by default. You can try to get a post to cache by using .ajax() and setting cache to true and type to post. I cannot say that this will work as typically you would not expect a post to cache. I suggest using get.

E.g

$.ajax( { url: '/bla',
          type : 'post',
          data : dataObj,
          cache : true } );
redsquare
i did that but by using get i can't get to the webmethod.
zaladane
AJAX Web Methods do not enable HTTP GET requests by default so you need the following attribute [ScriptMethod(UseHttpGet=true)]
redsquare
+1  A: 

Use the GET keyword instead if you want to use caching. There is a similar question on SO.

Arkain
A: 

I've noticed differences in the way ASP.NET caches responses depending upon whether the caching parameters are query string parameters or (in my case with ASP.NET MVC) route parameters.

I never completely figured out exactly what the conditions were, but this may help someone.

Simon_Weaver