通过AJAX设置时,不保留iOS 6 Mobile Safari会话变量

ebm*_*256 4 ajax session-variables mobile-safari ios6

可能重复:
iOS 6上的Safari缓存$ .ajax结果吗?

我在移动Safari iOS 6中遇到了通过AJAX设置的会话变量的问题.我提供了一个示例,它将设置会话变量,重定向到另一个页面,放弃会话并重新开始.这前两次工作正常.在第三次通过该过程时,会话变量丢失.问题只发生在iOS 6 safari中.它适用于我尝试过的所有其他浏览器.

该样本包含3页.Page1设置会话变量并重定向到第2页.Page 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>
Run Code Online (Sandbox Code Playgroud)

第1页Javascript:

function setSessionVariable() {
    PageMethods.SetSessionVariable(displaySetSessionVariable);
}

function displaySetSessionVariable(bReturn) {
    window.location = "Page2.aspx";
}
Run Code Online (Sandbox Code Playgroud)

第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;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

第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>
Run Code Online (Sandbox Code Playgroud)

代码:

namespace SafariSessionProblem {
    public partial class Page2 : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            lbl.Text = Session["TestVariable"].ToString();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第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>
Run Code Online (Sandbox Code Playgroud)

代码:

namespace SafariSessionProblem {
    public partial class Page3 : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            Session.Abandon();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tah*_*wan 5

我已经找到了问题,因为在IOS6中Safari缓存了ajax请求和响应,因此在向服务器发送请求时它使用缓存请求.为了解决这个问题,只需将时间戳添加到Ajax请求中,它就能正常工作.我已经放置了我用过的代码,在此帮助下更新了代码.希望对你有所帮助.

这是克服此问题的新变量parameters.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();
            })
        });
Run Code Online (Sandbox Code Playgroud)

谢谢