在ASP.NET WebForms中使用jQuery调用“ WebMethod” [英] Calling a 'WebMethod' with jQuery in ASP.NET WebForms

查看:96
本文介绍了在ASP.NET WebForms中使用jQuery调用“ WebMethod”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在下面的 WebMethod 中设置了一个断点,但是我从未遇到过断点。

I've set a breakpoint in the following WebMethod but I'm never hitting the breakpoint.

cs:

[WebMethod]
public static string search()
{
    return "worked";
}

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



<button id = "btnSearch" onclick = "search()" >Search</button>


推荐答案

请确保已在 ScriptManager 元素:

<asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true" />

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

and that you have canceled the default action of the button by returning false inside the onclick handler, otherwise the page performs a full postback and your AJAX call might never have the time to finish. Here's a full working example:

<%@ 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>

另一种可能性是毫不客气地订阅点击处理程序:

Another possibility is to subscribe to the click handler unobtrusively:

<button id="btnSearch">Search</button>

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

and then inside a separate javascript file:

$('#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;
});

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

You might also notice the usage of the msg.d property inside the success callback which ASP.NET uses to wrap the entire response into as well as the usage of the ResolveUrl method to properly generate the url to the page method instead of hardcoding it.

这篇关于在ASP.NET WebForms中使用jQuery调用“ WebMethod”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆