在ASP.NET WebForms中使用jQuery调用“WebMethod”

我在下面的WebMethod设置了一个断点,但是我从来没有碰到过断点。

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取消了该button的默认操作,否则该页面将执行完整的回发,并且您的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> 

另一种可能性是悄悄地订阅点击处理程序:

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

然后在一个单独的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; }); 

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

更优化的呼叫将是

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

根本不需要提供一个asp:ScriptManager

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

您当前的button正在导致完整的回发。 只需添加一个types=“button”到您的button,以避免这种情况。

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

-Shazzam哟

如何使用JQuery AJAX实现ASP.Net web方法?

HTML页面:

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="js/jquery.min.js"></script> <script> function SubmitData() { var name = 'Ram'; var gender = 'Male'; var age = '30'; $.ajax({ type: "POST", url: "ajaxcall.aspx/SaveData", data: '{"name":"' + name + '", "gender":"' + gender + '", "age":"' + age + '"}', contentType: "application/json; charset=utf-8", dataType: "json", beforeSend: function () { $('#loader').show(); }, success: function (data) { alert(data.d); $('#loader').hide(); }, error: function (msg) { //alert('3'); msg = "There is an error"; alert(msg); $('#loader').hide(); } }); } </script> </head> <body> <div id="loader" style="display: none;"> <img src="ajax-loader.gif" /> </div> <a href="#" onclick="SubmitData();">Submit</a> </body> </html> 

代码后面:

 [WebMethod] public static string SaveData(string name, string gender, string age) { try { return "OK"; } catch (Exception ex) { return ex.Message; } finally { } } 

资源: http : //www.sharepointcafe.net/2016/10/how-to-call-aspnet-web-method-using-jquery-ajax.html