在ASP.NET WebForms中使用jQuery调用'WebMethod'

mar*_*ery 22 asp.net ajax jquery webforms webmethod

我在下面设置了一个断点,WebMethod但我从来没有打过断点.

CS:

[WebMethod]
public static string search()
{
    return "worked";
}
Run Code Online (Sandbox Code Playgroud)

ASPX:

  function search() {
    $.ajax({
        type: "POST",
        url: "ProcessAudit/req_brws.aspx/search",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert(msg)
        }
    });
}
Run Code Online (Sandbox Code Playgroud)
<button id = "btnSearch" onclick = "search()" >Search</button>
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 28

确保您已在ScriptManager元素中启用了页面方法:

<asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true" />
Run Code Online (Sandbox Code Playgroud)

并且您已通过在onclick处理程序中返回false取消了该按钮的默认操作,否则该页面将执行完整的回发,并且您的AJAX调用可能永远没有时间完成.这是一个完整的工作示例:

<%@ Page Language="C#" %>
<script type="text/c#" runat="server">
[System.Web.Services.WebMethod]
public static string search()
{
    return "worked";
}
</script>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true" />
        <button id="btnSearch" onclick="search(); return false;" >Search</button>
    </form>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
        function search() {
            $.ajax({
                type: 'POST',
                url: '<%= ResolveUrl("~/default.aspx/search") %>',
                data: '{ }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {
                    alert(msg.d)
                }
            });
        }
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

另一种可能性是不引人注意地订阅点击处理程序:

<button id="btnSearch">Search</button>
Run Code Online (Sandbox Code Playgroud)

然后在一个单独的javascript文件中:

$('#btnSearch').click(function() {
    $.ajax({
        type: 'POST',
        url: '<%= ResolveUrl("~/default.aspx/search") %>',
        data: '{ }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            alert(msg.d)
        }
    });
    return false;
});
Run Code Online (Sandbox Code Playgroud)

您可能还会注意到msg.d成功回调中的属性的使用,ASP.NET使用该回调来包装整个响应以及方法的用法,ResolveUrl以正确生成页面方法的URL而不是对其进行硬编码.

  • 嗯... darins略有错误......根本不需要使用scriptmanager.也不需要charset,数据类型也是如此:json :) (7认同)

nav*_*een 6

更优化的呼叫将是

function search() {
    $.ajax({
        type: "POST",
        url: '<%= ResolveUrl("~/ProcessAudit/req_brws.aspx/search") %>',
        data: "{}",
        contentType: "application/json",
        success: function (msg) {
            msg = msg.hasOwnProperty("d") ? msg.d : msg;
            alert(msg);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

asp:ScriptManager根本不需要提供.

资源:http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

  • 由于涉嫌恶意软件,Chrome似乎不喜欢此链接,但我不想删除它,因为我没有发布此内容.https://www.virustotal.com/en/url/ba8f2c409307ec010ef80629f518aabb85e846e88881a113ba7c2d4259d62cfe/analysis/1485793115/ (3认同)