WebMethod not being called
Asked Answered
G

4

6

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");
    }
Giorgio answered 31/8, 2011 at 20:25 Comment(0)
P
6

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.

Plainlaid answered 31/8, 2011 at 20:38 Comment(0)
A
6

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);
    }
}
Archaeology answered 25/5, 2018 at 4:12 Comment(0)
C
0

i would want to add one note: you will have data error of your "ID" (or another field) string contains quotes like = '. solve this issue:

var DTO = {'sendData': ID};

                $.ajax({
                    "type": "POST",
                    "dataType": 'json',
                    "contentType": "application/json; charset=utf-8",
                    "url": sSource,
                    "data": JSON.stringify(DTO),
                    "success": function (msg) {
                        //do something
                    }
                });
Coenocyte answered 23/8, 2012 at 7:59 Comment(0)
A
0

Try like this: JQuery:

                var dataString = JSON.stringify({
                    contractName: contractName,
                    contractNumber: contractNumber
                });

                $.ajax({
                    type: "POST",
                    url: "CreateQuote.aspx/GetCallHistory",
                    data: dataString,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (result) {
                        alert(result);
                            OpenLightBox('divDelete');

                    }
                });

ASPX.CS:

        [System.Web.Services.WebMethod]
        public static string GetCallHistory(string contractName, string contractNumber)
        {
            return "Nalan";
        }
Abbreviation answered 18/6, 2013 at 3:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.