1

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 ...

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ak3nat0n
  • 6,060
  • 6
  • 36
  • 59

3 Answers3

3

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
  • 78,161
  • 20
  • 151
  • 159
  • i did that but by using get i can't get to the webmethod. – ak3nat0n Aug 05 '09 at 21:02
  • 1
    AJAX Web Methods do not enable HTTP GET requests by default so you need the following attribute [ScriptMethod(UseHttpGet=true)] – redsquare Aug 05 '09 at 21:45
  • Well using Get doesn't solve any thing its till not cached, and if we use CacheDuration then we get server side caching not client side caching.. – Peter Jul 31 '13 at 11:34
1

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

Community
  • 1
  • 1
Jesper Fyhr Knudsen
  • 7,802
  • 2
  • 35
  • 46
0

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
  • 140,023
  • 84
  • 646
  • 689