locked
Problem Calling C# Function Using Ajax In Webforms RRS feed

  • Question

  • User1349647816 posted

    Hi,

    I am currently using webforms and need to call a function in the .cs file using ajax, the problem is the success function get called with ajax but it doesn't hit the function in any way. 

    Also I have a custom string that I am sending to the function but the ajax is sending the entire html markup instead of just the json string.

    Here's my function in the .cs file

    [WebMethod]
    public static void MyFunction(string jstring = "")
    {
       string what = "what";
    }

    and here's my jquery ajax code

    $("#btnSendAjax").on('click', function (e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "Default.aspx/MyFunction",
            data: { jstring: "testString" },
            })
            .done(function (data) {
                console.log("success");
                console.log(data); // This returns the whole html page's markup
                    
            })
            .fail(function (jqXHR, textStatus, c) {
                console.log("failure");
                console.log(textStatus);
            });
    
    });

    How do pass my json string via ajax to my c# function?

    Saturday, August 8, 2020 8:06 AM

Answers

  • User475983607 posted

    Disable friendly URLs in the App_Start/RouteConfig.cs file.

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

    Send JSON

    <script>
        $("#<%=btnSendAjax.ClientID%>").on('click', function (e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "default.aspx/MyFunction",
                data: JSON.stringify({ jstring : "testString" }),
            })
                .done(function (data) {
                    console.log("success");
                    console.log(data); // This returns the whole html page's markup
                })
                .fail(function (jqXHR, textStatus, c) {
                    console.log("failure");
                    console.log(textStatus);
                });
    
        });
    </script>

    Return a value from the web method.

    [WebMethod]
    public static string MyFunction(string jstring)
    {
        return "You send " + jstring;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 8, 2020 11:51 AM

All replies

  • User475983607 posted

    Disable friendly URLs in the App_Start/RouteConfig.cs file.

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

    Send JSON

    <script>
        $("#<%=btnSendAjax.ClientID%>").on('click', function (e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "default.aspx/MyFunction",
                data: JSON.stringify({ jstring : "testString" }),
            })
                .done(function (data) {
                    console.log("success");
                    console.log(data); // This returns the whole html page's markup
                })
                .fail(function (jqXHR, textStatus, c) {
                    console.log("failure");
                    console.log(textStatus);
                });
    
        });
    </script>

    Return a value from the web method.

    [WebMethod]
    public static string MyFunction(string jstring)
    {
        return "You send " + jstring;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 8, 2020 11:51 AM
  • User1349647816 posted

    Thank you so much I've been struggling with this for days, can you explain to me what exactly I was doing wrong and why is this line 

    data: JSON.stringify({ jstring : "testString" }),

    not the same as 

    data: JSON.stringify(myJSONstring),

    Saturday, August 8, 2020 12:42 PM
  • User475983607 posted

    I have no idea what the variable "myJSONstring" contains but if it is a JSON string the you are double serializing.  Always read the reference documentation when you run across a library or function you have not used.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

    Saturday, August 8, 2020 1:06 PM