Prevent iOS 6 from Caching Ajax POST Requests [duplicate]

南笙酒味 提交于 2020-01-01 09:25:10

问题


Possible Duplicate:
Is Safari on iOS 6 caching $.ajax results?

I have a hybrid application using PhoneGap that runs fine on Android and iOS. But when I started testing in iOS 6 I noticed that I am not getting server data for most of my ajax calls - instead I was getting the cached data from previous ajax calls.

So far I have tried the following options to disable cache -

  1. Include a timestamp as query string parameter
  2. $.ajaxSetup({ cache: false });
  3. Inside the ajax call no-cache = true
  4. $.ajaxPrefilter(function (options, originalOptions, jqXHR) { options.data = jQuery.param($.extend(originalOptions.data||{}, { timeStamp: new Date().getTime() })); });

But none of these seems to be working. I am invoke Java action classes from my ajax calls - will it have something to do with the reason why the methods listed above are failing?


回答1:


Read this thread

Is Safari on iOS 6 caching $.ajax results?

You could disable the caching on webserver level and by using timestamps in the URL.




回答2:


How to fix it: There are various methods to prevent caching of requests. The recommended method is adding a no-cache header.

This is how it is done.

jQuery:

Check for iOS 6.0 and set Ajax header like this.

$.ajaxSetup({ cache: false });

ZeptoJS :

Check for iOS 6.0 and set Ajax header like this.

$.ajax({
type: 'POST',
headers : { "cache-control": "no-cache" },
url : ,
data:,
dataType : 'json',
success : function(responseText) {…}

Server side

Java :

httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");

Make sure to add this at the top the page before any data is sent to the client.

.NET

Response.Cache.SetNoStore();

Or

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

PHP

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.


来源:https://stackoverflow.com/questions/12796318/prevent-ios-6-from-caching-ajax-post-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!