开发者

iPad caching AJAX responses despite setting 'No Store'

I have a page which does an AJ开发者_如何学JAVAAX 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?


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜