作者:陈hancox_894 | 来源:互联网 | 2023-05-19 18:42
PossibleDuplicate:IsSafarioniOS6caching$.ajaxresults?可能重复:ios6缓存的Safari$。ajax的结果吗?
Possible Duplicate:
Is Safari on iOS 6 caching $.ajax results?
可能重复:ios6缓存的Safari $。ajax的结果吗?
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.
我有一个使用PhoneGap的混合应用,在Android和iOS上运行良好。但是当我在ios6中开始测试时,我注意到我的大部分ajax调用都没有得到服务器数据——相反,我是从以前的ajax调用中获取缓存的数据。
So far I have tried the following options to disable cache -
到目前为止,我已经尝试了以下选项来禁用缓存-
- Include a timestamp as query string parameter
- 包含时间戳作为查询字符串参数
$.ajaxSetup({ cache: false });
- 美元。ajaxSetup({缓存:假});
- Inside the ajax call
no-cache = true
- 在ajax调用中,no-cache = true。
$.ajaxPrefilter(function (options, originalOptions, jqXHR) { options.data = jQuery.param($.extend(originalOptions.data||{}, { timeStamp: new Date().getTime() })); });
- 美元。ajaxPrefilter(函数(选项、原始选项、jqXHR){选项。data = jQuery.param(.extend美元(originalOptions。数据||{},{时间戳: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?
但这些似乎都没有起作用。我正在从ajax调用中调用Java操作类——它是否与上面列出的方法失败的原因有关?
2 个解决方案
4
From here
从这里
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:
jQuery:
Check for iOS 6.0 and set Ajax header like this.
检查iOS 6.0并设置Ajax header如下。
$.ajaxSetup({ cache: false });
ZeptoJS :
ZeptoJS:
Check for iOS 6.0 and set Ajax header like this.
检查iOS 6.0并设置Ajax header如下。
$.ajax({
type: 'POST',
headers : { "cache-control": "no-cache" },
url : ,
data:,
dataType : 'json',
success : function(responseText) {…}
Server side
服务器端
Java :
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
net
Response.Cache.SetNoStore();
Or
或
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
PHP
PHP
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.