4

I have a page which does an AJAX request to get data, which is then modified by the user. When using the iPad, if the user makes a change, then goes away from the page, then comes back - the AJAX requests are not re-run - meaning the old data is loaded (presumably from the iPad's browser cache).

My server code is ASP.NET Forms, and I have tried setting no cache with all the settings I can find:

Context.Response.Cache.SetNoStore()
Context.Response.Cache.SetETag(New Guid().ToString())
Context.Response.Cache.SetExpires(New DateTime())
Context.Response.Cache.SetNoServerCaching()
Context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
Context.Response.Expires = 0

But no luck. Any ideas?

Ian Grainger
  • 5,148
  • 3
  • 46
  • 72

2 Answers2

5

As proposed in the previous answer, jQuery provides a simple option cache for it's $.ajax method:

If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL.

So for example:

$.ajax({
  url: "test.html",
  cache: false,
  success: function () {
    $(this).addClass("done");
  }
});

If you are not using jQuery you can implement a similar behavior on your own.

cburgmer
  • 2,150
  • 1
  • 24
  • 18
  • +1 very interesting. I'm already using the solution I listed in my answer, but if I were to do this again (considering I *do* use jQuery) I'd definitely use this answer. – Ian Grainger Jan 10 '12 at 07:07
  • This answer does not work with iOS 6, see here: http://stackoverflow.com/questions/12506897/is-safari-on-ios-6-caching-ajax-results – Brian Ogden Apr 29 '13 at 06:15
0

My current fix is to constantly (slightly) modify the URL of the AJAX request:

url: "/AjaxService.asmx/" + methodName + "?dt=" + new Date().getTime()

Because the querystring is passed to the same web method each time, I can ignore it's value, but Safari can't cache the result, because it's always at a different address.

Surely this isn't the best solution, though!

Ian Grainger
  • 5,148
  • 3
  • 46
  • 72
  • It’s a very common solution, and in my book it is surely better than replacing `GET` with `POST`. – Vasiliy Faronov Jul 14 '11 at 11:38
  • I didn't realise it was common. Thanks for the reassurance :) – Ian Grainger Jul 14 '11 at 12:02
  • @Vasiliy Faronov: There are *many* reasons to use POST instead of GET. To name a few: 1) If you are passing data, GET puts parameters in the Query String; which doesn't handle Unicode / UTF-8 text very well, or at least uniformly. Servers typically don't handle something like Cyrillic very well in the URL. A POST does not have that limitation. 2) If you don't want it to cache; any many others... Now, that isn't to say GET is bad; but there are plenty of reasons to use POST. – vcsjones Jul 14 '11 at 14:15
  • I'm not sending information. So the _only_ reason to use POST is to avoid caching? Well in that case this solution seems just as good - or have I missed something? – Ian Grainger Jul 16 '11 at 08:10
  • Downvoted because you can set "cache:false" in the Ajax request (see answer above). Your propose solution is clearly a code smell. – Tamas Ionut Oct 08 '13 at 10:09
  • @TamasIonut Oh. Er. OK. – Ian Grainger Oct 08 '13 at 14:52