Asked by:
Ajax call not hitting code behind c# method

Question
-
User370111851 posted
jQuery Ajax Call
<script type="text/javascript"> $(document).ready(function () { $('#MainContent_minuteBooks').click(function (e) { e.preventDefault(); $.ajax({ type: "GET", url: "MainView.aspx/GetPageTypes", error: function (XMLHttpRequest, textStatus, errorThrown) { console.log("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown); }, success: function (response) { console.log("--" + JSON.stringify(response)); } }); }); }); </script>
CodeBehind WebMethod
[WebMethod] public static string GetPageTypes() { List<string> pages = new List<string>(); string html = ""; Console.WriteLine(book.Count); foreach(MinuteBookPage mbp in book) { if (!pages.Contains(mbp.Class)) { pages.Add(mbp.Class); }; } foreach(string s in pages) { html += "<div class='page' style='border: solid 2px red'><p>" + s + "</p></div>"; } Console.WriteLine(html); return html; }I have breakpoints set on the method, as well as a Console.WriteLine that should be display, the breakpoints are not hit, and the console does not output anything
I am lead to believe that my method is not being called
Upon success the ajax call returns the html and JavaScript markup for the page i am on
I have looked at this link but it seems outdated, and all other resources ive looked at dont seem to follow what it outlines
Tuesday, September 24, 2019 3:47 PM
All replies
-
User475983607 posted
Do a POST not a GET. Secondly, you are returning HTML not JSON.
$.ajax({ type: "POST", url: "MainView.aspx/GetPageTypes", error: function (XMLHttpRequest, textStatus, errorThrown) { console.log("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown); }, success: function (response) { console.log("--" + response); } });
Use the ClientID to make sure you are referencing the server control correctly. This assume the server control is named minuteBooks.
$('#<%=minuteBooks.ClientID%>').click(function (e) {
Tuesday, September 24, 2019 5:51 PM -
User-2054057000 posted
I have breakpoints set on the method, as well as a Console.WriteLine that should be display, the breakpoints are not hit, and the console does not output anything
I am lead to believe that my method is not being called
Implement the error callback method to find out what error you get in your ajax() call.
error: function (req, status, error) { alert(error + " " + req + " " + status); }
Take help form this tutorial - jQuery AJAX Example – Calling a C# Function With jQuery AJAX [With Code]
Wednesday, September 25, 2019 9:51 AM -
User288213138 posted
Hi a.singh18,
I have breakpoints set on the method, as well as a Console.WriteLine that should be display, the breakpoints are not hit, and the console does not output anythingAs far as i know, Console.Write will not work in ASP.NET.
You can refer to this link:
https://stackoverflow.com/a/9614249
I am lead to believe that my method is not being calledYou can't dubug your [webmethod], If you want to know if webmethod is called, you can view it in your success: function().
Best regards,
Sam
Friday, October 25, 2019 9:59 AM -
User-258456925 posted
I guess you're creating minuteBooks elements dynamically. So instead of direct click event attach in document like follow:
$(document).on('click', '#MainContent_minuteBooks', function (e) { //your code.. });
If it's not created dynamically then try this calling server call from client and see if it works and accordingly adjust your code.
Friday, October 25, 2019 2:53 PM -
User702049738 posted
Good advice
Friday, October 25, 2019 4:57 PM