0

I have a 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>

This should call my code behind 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

In the network tab this is displayed, First call , there is a second call after this 301 response, and it just returns the page html/javascript

Upon success the ajax call returns the html and JavaScript markup for the page i am on

I have looked at this link Pagemethods in asp.net but it seems outdated, and all other resources ive looked at dont seem to follow what it outlines

Ajay Singh
  • 63
  • 10
  • Alright so ive changed the url to ```url: "MainView/GetPageTypes"``` the 301 is no longer happening, but still no breakpoints are hit in the WebMethod – Ajay Singh Sep 24 '19 at 16:31
  • The method is kept in the codebehind. So MainView.aspx has my ajax call, and MainView.aspx.cs holds the method im trying to reach. Im not using a MVC type methodology – Ajay Singh Sep 24 '19 at 16:55
  • 1
    Ah, sorry, I missed that tag. In that case, you will want `.aspx` in your URL. Check out this link: https://stackoverflow.com/questions/6928533/calling-a-webmethod-with-jquery-in-asp-net-webforms It probably sounds weird, but try adjusting your ajax call to model the one mentioned in the answer (with `type; 'POST'` and all of that). – Dortimer Sep 24 '19 at 17:06
  • @Dortimer With a WebMethod, there is no controller. The WebMethod is declared on a .aspx page and accessed via the URL of the page. You should remove the comments that are no longer relevant. – mason Sep 24 '19 at 17:10
  • Following that post made, and turning ```AutoRedirectMode``` to off i was able to get it to call the function. Thank you. I will post my final call so others can see what it looks like – Ajay Singh Sep 24 '19 at 17:33

1 Answers1

0

Thanks to @Dortimer for pointing me to this post, and helping me work out the issue.

How to properly make an ajax call with webforms

I ended up with the following code

Ajax call

<script type="text/javascript">
        $(document).ready(function () {
            $('#MainContent_minuteBooks').click(function () {
                console.log("inside click");

                $.ajax({
                    type: 'POST',
                    url: '<%= ResolveUrl("~/MainView.aspx/GetPageTypes") %>',
                    data: '{ }',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (msg) {
                        console.log(msg)
                    }
                });
                return false;
            });
        });
    </script>

Also i had to turn off RedirectMode inside of the RouteConfig.cs file

        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Off;
            routes.EnableFriendlyUrls(settings);
        }
Ajay Singh
  • 63
  • 10