【问题标题】:Calling Server method by jquery is not working通过 jquery 调用服务器方法不起作用
【发布时间】:2018-12-15 03:20:42
【问题描述】:

我有一个确认对话框,我想从 jquery 中调用一个简单的方法。
当我点击“否”时,它应该调用服务器方法。
我用过:

function ConfirmDialog() {
        $('<div></div>').appendTo('body')
.html('<div><h4>' + 'Do you wanto to see previous data' + '?</h4></div>')
.dialog({
    modal: true, title: 'Carry forward!', zIndex: 10000, autoOpen: true,
    width: 'auto', resizable: false,
    buttons: {
        Yes: function () {
            //                $(obj).removeAttr('onclick');
            //                $(obj).parents('.Parent').remove();
             <%= ClientScript.GetPostBackEventReference(btnConfrm, Nothing) %>                           
            $('body').append('<h1>Confirm Dialog Result: <i>Yes</i></h1>');
            $(this).dialog("close");
        },
        No: function () {
             $.ajax({
                type: "POST",
                url: "MPR_Fill.aspx/dataNotCarryForwarding",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function(response) {

                    }
                });                      
            $('body').append('<h1>Confirm Dialog Result: <i>No</i></h1>');
            $(this).dialog("close");
        }
    },
    close: function (event, ui) {
        $(this).remove();
    }

方法是:

Public Sub dataNotCarryForwarding()

    Response.Write("In dataNotCarryForwarding()")

End Sub
Public Sub dataCarryForwarding()

    gvCustomers.Visible = True
    fillGridWithCarryForwarding()
End Sub

按钮是:

<asp:Button ID="btnConfrm" runat="server" Text="View" ForeColor="Black" Width="80px"
                            CssClass="button" Height="30px" ValidationGroup="btn" OnClientClick="ConfirmDialog(); return false;" OnClick="dataCarryForwarding" />
<asp:Button ID="btn_show" runat="server" Text="View" ForeColor="Black" Width="80px"
                            CssClass="button" Height="30px" ValidationGroup="btn" OnClick="fillGridWithoutCarryForwarding" />

No:function() 直接调用dataNotCarryForwarding() 不起作用。

【问题讨论】:

  • ajax is not working. 是什么意思。你得到什么错误
  • @Sajidkhan 您将Ajax 声明为“POST”类型,但您没有向服务器发布任何数据
  • 您可以在成功后添加 , error: function(jqXHR, textStatus, errorThrown ) 以获取更多信息...并在“失败”时打印响应
  • 在C#中,需要在ajax可访问的服务器方法上方添加标签[WebMethod]。在 VB 中也可能需要类似的东西?

标签: jquery asp.net vb.net


【解决方案1】:

您需要Imports System.Web.Services 并将&lt;WebMethod&gt; 属性添加到您的代码隐藏函数中:

<WebMethod>
Public Sub dataNotCarryForwarding()

    Response.Write("In dataNotCarryForwarding()")

End Sub

【讨论】:

    猜你喜欢
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多