Browser caching

Jun 30, 2009 at 6:05 PM
Edited Jun 30, 2009 at 6:06 PM

It appears that the browser is caching the results of web service calls I am making using the XmlWebPart.

I am reloading my XML div upong user request using the following code...

var xmlComponent = element.XmlComponent;
xmlComponent.lastModXml =
null;
xmlComponent.lastMod =
null;
xmlComponent.Reload();

I tried adding the following headers in SharePoint.ajax.js when the web request is issued...

request.get_headers["pragma"] = "no-cache";
request.get_headers["cache-control"] = "no-cache";

But the result which is retrieved in the DataLoadHandler is always stale if browser caching is enabled.

Has anyone seen this behavior and found a workaround?

I am using Sharepoint.Ajax.3.0.2 ootb (latest version?)

Thanks!
Eric

 

Jul 1, 2009 at 4:16 PM

The answer is on pg 71 of Daniel's book (Developing Service-Oriented AJAX Applications on the Microsoft Platform) suggesting that the following code needs to be included in the server side response.  The Tip on the following page sums it up... if you do not explicitly expire the reponse, the browser might cache the data indefinitely, depending on the browser's cache settings. 

HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddYears(-1));

Perhaps there is still a webrequest header or other setting I can modify in client side to force a refresh, but this worked since I have control over the web service I'm calling.

btw, I did upgrade to the latest build (52849) from the older v3.0.2 release build I had been using without any issues.

All is well.

 

 

Thanks,

Eric

 

 

Coordinator
Jul 1, 2009 at 4:29 PM

Sweet- I'm glad you found that! Yeah, you HAVE to set something on the server to keep the client from caching.

As an alternative, you can always change the URL by appending a query string. We do that at NewsGator for each build, so a cached version of AJAX data is not used with a newer release (where the data may be incomplete or invalid).

Also-- I do recommend getting the SOURCE CODE and using that, as sometimes my "Releases" are out of date. I did make some considerable enhancements to the XmlComponent since the code you're referencing. :)