4

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

I have been having a problem with session variables set via AJAX in mobile Safari iOS 6. I included a sample that will set a session variable, redirect to another page, abandon the session and start over. This works fine the first 2 times. On the third time through the process, the session variables get lost. The problem only happens in iOS 6 safari. It works in all other browsers I have tried.

The sample consists of 3 pages. Page1 sets the session variable and redirects to Page 2. Page 2 displays the session variable. Page 3 abandons the session variable.

Page 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>

Page 1 Javascript:

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

function displaySetSessionVariable(bReturn) {
    window.location = "Page2.aspx";
}

Page 1 Code:

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

}

Page 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>

Page 2 Code:

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

Page 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>

Page 3 Code:

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

1 回答 1

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();
            })
        });

谢谢

于 2012-09-25T14:54:16.850 回答