Hi
I am trying below code but WebMethod not getting called
JavaScript
function GetModuleData(e) {
var currentRow = $(e).closest("tr");
var rp;
// rp = currentRow.find("td:eq(0)").text();
var table = document.getElementById("grdPlanning"), rIndex, cIndex, track;
// table rows
for (var i = 1; i 0) {
Mobile = $('table tr th:eq(' + (cIndex) + ')').text();
BookId = currentRow.find("td:eq(0)").text();
var str = Mobile;
const result = str.split(/[()]/);
var res1 = result[0];
var res2 = result[1];
document.getElementById('hdfBookId').value = BookId;
document.getElementById('hdfMobile').value = res2;
};
};
}
}
$.ajax({
type: "POST",
url: "AddRecommendedBooks.aspx/getPopUp",
/* *//*data: JSON.stringify({ bookId: BookId, mobile: res2 }),*/
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// Handle the response if needed
console.log("C# code executed successfully.");
},
error: function (xhr, textStatus, errorThrown) {
// Handle the error if needed
console.log("Error executing C# code:", errorThrown);
}
});
};C#
[WebMethod]
protected void getPopUp()
{
try
{
BALBooks bALBooks = new BALBooks();
BALStudents bALStudents = new BALStudents();
StudentDetail Result = bALStudents.GetStudentDetailByLoginID(bALStudents.GetStudentIDByMobile(hdfMobile.Value));
ltrlHead.Text = Result.Name;
ltrlHead.Text = ltrlHead.Text + " : " + bALBooks.GetBookTitle(Convert.ToInt32(hdfBookId.Value));
BindData(Convert.ToInt32(hdfBookId.Value), Result.LoginCode);
}
catch (Exception ex)
{
Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfLoginCode.Value));
ShowMessage("Oops...", ex.Message, "error");
}
}Thanks

Amit MohantyRank #112 · 20K reputationPosted May 16, 2023, 9:22 AM EDT
Make sure that the
AddRecommendedBooks.aspxpage has theScriptManagercontrol added to it. TheScriptManagercontrol is required for enabling ASP.NET AJAX functionality.Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 16, 2023, 9:24 AM EDT
it seems that you are attempting to call a WebMethod named
getPopUpfrom yourAddRecommendedBooks.aspxpage using AJAX. However, there are a few points to consider:WebMethod Accessibility: Ensure that the
getPopUpmethod is declared aspublic staticin your code-behind file (code-behind forAddRecommendedBooks.aspx).ScriptManager: If you are using an ASP.NET Web Forms application, make sure you have a ScriptManager control on your page. The ScriptManager enables the page to handle AJAX requests.
PageMethods: If you're not using a ScriptManager, you can try using the
PageMethodsobject to call the WebMethod. Modify your AJAX call as follows:$.ajax({ type: "POST", url: "AddRecommendedBooks.aspx/getPopUp", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { // Handle the response if needed console.log("C# code executed successfully."); }, error: function (xhr, textStatus, errorThrown) { // Handle the error if needed console.log("Error executing C# code:", errorThrown); } });Replace the above AJAX call with the following code:
PageMethods.getPopUp(onSuccess, onError); function onSuccess(response) { // Handle the response if needed console.log("C# code executed successfully."); } function onError(xhr, textStatus, errorThrown) { // Handle the error if needed console.log("Error executing C# code:", errorThrown); }Ensure that the
EnablePageMethodsattribute is set totruein your ScriptManager or in the page directive (EnablePageMethods="true").Additionally, make sure your web application is running on a server that supports ASP.NET AJAX, as the WebMethod call won't work with static HTML files.