4

重複の可能性:
iOS6のSafariは$.ajaxの結果をキャッシュしていますか?

モバイルSafariiOS6でAJAXを介して設定されたセッション変数に問題があります。セッション変数を設定し、別のページにリダイレクトし、セッションを中止して最初からやり直すサンプルを含めました。これは最初の2回は正常に機能します。プロセスの3回目で、セッション変数が失われます。この問題は、iOS6サファリでのみ発生します。それは私が試した他のすべてのブラウザで動作します。

サンプルは3ページで構成されています。ページ1はセッション変数を設定し、ページ2にリダイレクトします。ページ2はセッション変数を表示します。ページ3はセッション変数を放棄します。

ページ1HTML:

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

ページ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;
        }
    }

}

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>

ページ2コード:

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>

ページ3コード:

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 に答える