Saturday, March 2, 2013

Safari on iOS 6 caching $.ajax results?

If your web application serves iphone/iPad users (with iOS 6), the below is one of the issue you could see:

I one of the application that I was working on, we saw that the information were not returned back from the ajax call back to the UI. But we could see that from the ASP.NET MVC application the results were being sent back. It was just that the UI wouldnt refresh!!  

We use $.ajax (jQuery) call to perform Refresh operation (to poll and get the results back to the UI). And  I always believed that a POST action would never be cached. Either call it  Over engineering or, User experience here you have!! Safari browser (iOS 6) caching your results (In our case the application was returning back “no data” for first couple of poll and, safari cached that result).

It’s been argued that this is actually a bug in iOS6 and not a feature.

The W3 spec says:

“Responses to [POST] are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields.”

But apple is caching everything unless you say otherwise – the exact opposite of the spec. *sigh*


So you have few options to resolve this:

1) One solution is to check the cache-control directive in response header served up by your application. It should be set to no-cache. It is this that tells the client (and any intermediate proxy servers whether the response can be cached).
2)  Set cache-control to “no-cache” and, Cache =false in your ajax call request. With this being set, iOS6 safari won’t cache your response.
3)   Change your post data every time you poll, like passing random data (Time for example) would also prevent the result being cached (Sounds more like a Hack rather than solution).

Check the post for more information. Hope this would save someone few hours of agony.

Happy Debugging!!

No comments: