WebMethod not being called

11,047

Solution 1

Try following fixes for your Ajax request:

 $.ajax({
            type: "post",
            url: "Playground.aspx/childBind",
            data: "{sendData: '" + ID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) { alert("successful!" + result.d); }
        })

Notice changed dataType and data value as a string.

Solution 2

I have encountered the same issue. After Googling, I found the solution, and it works for me. Navigate to RouteConfig.cs and comment out the line below:

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        //settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}
Share:
11,047
Seraph812
Author by

Seraph812

Updated on August 16, 2022

Comments

  • Seraph812
    Seraph812 almost 2 years

    I am passing a javascript variable containing a string to the server via jquery.ajax. Although the "success" condition is called, the server-side WebMethod is never called. Client:

     $.ajax({
                type: "post",
                url: "Playground.aspx/childBind",
                data: {sendData: ID},
                //contentType: "application/json; charset=utf-8",
                dataType: "text",
                success: function (result) { alert("successful!" + result.d); }
            })
    

    Server:

    [WebMethod]
        public static string childBind(string sendData)
        {
            return String.Format("Hello");
        }