【问题标题】:iOS 6 Mobile Safari Session Variables not retained when set via AJAX [duplicate]通过 AJAX 设置时未保留 iOS 6 Mobile Safari 会话变量 [重复]
【发布时间】:2012-09-16 06:34:02
【问题描述】:

可能重复:
Is Safari on iOS 6 caching $.ajax results?

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

示例由 3 页组成。 Page1 设置会话变量并重定向到第 2 页。第 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 会话变量类似的问题..

标签: ajax mobile-safari session-variables ios6


【解决方案1】:

我已经解决了这个问题,因为在 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();
            })
        });

谢谢

【讨论】:

  • 这解决了问题。感谢您的回复。
猜你喜欢
  • 2016-03-20
  • 2017-01-02
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2011-12-07
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多