可能重复:
iOS 6上的Safari是否正在缓存$.ajax结果?
我在mobile Safari iOS 6中通过AJAX设置会话变量时遇到了一个问题。我包括了一个示例,它将设置一个会话变量,重定向到另一个页面,放弃会话并重新开始。前两次都很好用。在第三次通过进程时,会话变量丢失。该问题只发生在iOS 6 Safari中。它在我尝试过的所有其他浏览器中都能工作。
样本共3页。Page1设置会话变量并重定向到Page2。第2页显示会话变量。第3页放弃会话变量。
第1页HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/Page1.js" />
</Scripts>
</asp:ScriptManager>
<div onclick="setSessionVariable()">Set session variable and redirect to page 2</div>
</form>
</body>
</html>
第1页JavaScript:
function setSessionVariable() {
PageMethods.SetSessionVariable(displaySetSessionVariable);
}
function displaySetSessionVariable(bReturn) {
window.location = "Page2.aspx";
}
第1页代码:
using System.Web.Services;
namespace SafariSessionProblem {
public partial class Page1 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
[WebMethod]
public static Boolean SetSessionVariable() {
System.Web.HttpContext.Current.Session["TestVariable"] = 1;
return true;
}
}
}
第2页HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lbl" runat="server" Text="Label"></asp:Label><br /><br />
<div onclick="window.location = 'Page3.aspx'">Redirect to page 3 and abondon session</div>
</form>
</body>
</html>
第2页代码:
namespace SafariSessionProblem {
public partial class Page2 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
lbl.Text = Session["TestVariable"].ToString();
}
}
}
第3页HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div onclick="window.location = 'Page1.aspx'">Start over</div>
</form>
</body>
</html>
第3页代码:
namespace SafariSessionProblem {
public partial class Page3 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
Session.Abandon();
}
}
}
我已经解决了这个问题,因为在IOS6中Safari缓存了ajax请求和响应,所以在向服务器发送请求时使用缓存的请求。为了克服这个问题,只需在Ajax请求中添加时间戳,就可以很好地工作。我已经放置了我使用过的代码,在此帮助下更新您的代码。希望会帮助你。
这是一个用来克服这个问题的新变量参数s.timestamp=new Date()。getTime();
parameters.qString = location.hash;
parameters.timeStamp = new Date().getTime();//this new line for safari issue
application.ajax({
url: application.ajaxUrl + "ajax",
type: "post",
dataType: "json",
data: $.extend({method:parameters.method}, parameters),
success: function( response ){
stop_spinner();
})
});
谢谢