【问题标题】:Ajax error using POST request with Ajax control tool kit使用带有 Ajax 控制工具包的 POST 请求的 Ajax 错误
【发布时间】:2015-02-18 11:05:15
【问题描述】:

我的模态弹出会话超时扩展器行为不端。

我正在尝试实施上一个问题 (Maintaining page number in DropDownList after postback to refresh server timeout) 中给出的解决方案。

我遇到一个错误:尝试使用 POST 请求调用方法 \u0027Check\u0027,这是不允许的。"," 我一直在研究该错误,简单的解决方案似乎是该请求不是发布请求。我试过删除 runat="server",添加 "return:false;"到 onClick,并在脚本中添加“return false”。

有没有办法让它工作?

我的 .asmx 网络文件:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following    line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class SessionKeepAlive
    Inherits System.Web.Services.WebService

    <WebMethod(EnableSession:=True)> _
    <Script.Services.ScriptMethod(UseHttpGet:=True)> _
    Public Function Check() As String
        If HttpContext.Current.Session("username") Is Nothing Then
            Return "expired"
        Else
            Return "ok"
        End If
    End Function

End Class

我的弹出html:

<asp:Panel ID="pnlSessionTimeout" runat="server" CssClass="modalPopup" Style="display: none;"
Width="300">
    <table style="width: 100%;">
        <tr>
            <td style="color: #FFFFFF; font-weight: bold; background-color: #51516A" align="center">
                WARNING! Session About to Expire
            </td>
        </tr>
        <tr>
            <td>
                <p>
                    Your session will time out in
                    <asp:Label ID="lblTimeoutCount" runat="server" Text=""></asp:Label>
                    minutes. Click the ‘Continue’ button to keep working. 
                    When the timer reaches 0:00 your online session is no longer active.
                    <asp:UpdatePanel ID="updpnlCountDown" runat="server">
                        <ContentTemplate>
                            <span id="CountDownHolder"></span>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                    <br />
                    <br />
                    <span style="text-align: center; display: block;">
                        <asp:Button ID="btnPreserveSession" runat="server" CausesValidation="false" Text="Continue"/>
                    </span>
                    <br />
                </p>
            </td>
        </tr>
    </table>
</asp:Panel>
<cc1:ModalPopupExtender ID="mpeSessionTimeout" runat="server" TargetControlID="pnlSessionTimeout"
    BackgroundCssClass="modalBackground" BehaviorID="mdlSessionTimeout" DropShadow="false"
    OkControlID="btnPreserveSession" OnOkScript="PreserveSession()"     PopupControlID="pnlSessionTimeout">
</cc1:ModalPopupExtender>

我的 ajax 脚本调用:

    function PreserveSession() {
    {
        $.ajax({
            type: "POST",
            url: "SessionKeepAlive.asmx/Check",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            success: checkAuthenticatedOk,
            error: checkAuthenticatedError
        });

    }

【问题讨论】:

    标签: javascript asp.net vb.net asp.net-ajax


    【解决方案1】:

    这不是您问题的直接答案。但是,由于您使用的是 Telerik Ajax,因此您不必重新发明轮子。

    你可以使用Session Timeout Notification

    【讨论】:

    • 我调查了这个,我们有 ajax 控制工具包,而不是 telerik ajax ui。我对此很陌生,也许我误解了一些东西,但这就是我尝试实施此解决方案时所说的
    • 我在你的代码中没有看到 Telerik;可能只是ASP.NET AJAX Control Toolkit。顺便说一句,你怎么知道你的应用程序使用 Telerik?
    • 你是对的 @Win 很抱歉我一定在我的其他项目之一中看到了它并且这个名字卡在了我的脑海中。
    【解决方案2】:

    为了使用来自 AJAX 的 POST 请求,我必须将 (UseHttpGet:=True) 更改为 False。 UseHttpGet:=True 只能用于 AJAX GET 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-11
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 2018-02-17
      相关资源
      最近更新 更多