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>
Make sure that you have enabled page methods in your
ScriptManager
element: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:
Another possibility is to subscribe to the click handler unobtrusively:
and then inside a separate javascript file:
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 theResolveUrl
method to properly generate the url to the page method instead of hardcoding it.A more optimised call will be
No need to provide a
asp:ScriptManager
at all.Resource: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
How to implement ASP.Net web method using JQuery AJAX ?
HTML Page:
Code behind:
Resource: http://www.sharepointcafe.net/2016/10/how-to-call-aspnet-web-method-using-jquery-ajax.html
Your current button is causing a full postback. Simply add a type="button" to your button to avoid this.
-Shazzam yo