javascript - 使用 Ajax 控制工具包使用 POST 请求时出现 Ajax 错误

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

我的模式弹出 session 超时扩展器行为不当。

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

我遇到错误:尝试使用 POST 请求调用方法\u0027Check\u0027,这是不允许的。","我一直在研究该错误,简单的解决方案似乎是该请求不是 post 请求。我尝试删除 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
        });

    }

最佳答案

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

您可以使用 Session Timeout Notification .

enter image description here

关于javascript - 使用 Ajax 控制工具包使用 POST 请求时出现 Ajax 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27567276/

相关文章:

javascript - JavaScript 中匿名函数作为包装器的必要性

javascript - 从数组列表渲染列表

asp.net - 站点如何在线构建 zip 文件以供下载?

vb.net - 是否有必要在我的响应 header 中设置内容长度?

vb.net - vb 计数奇怪的行为

javascript - 如何在 Jest 测试中为无状态函数的输入文本元素设置值

javascript - 使用 AJAX JQuery 访问/处理(嵌套)对象、JSON 中的数组

css - 如何将 CSS 样式添加到 asp.net ListView ?

.net - 在 Web 应用程序中管理多种 .NET 语言

vb.net - 委托(delegate)和地址