Seraph812
Seraph812

Reputation: 417

WebMethod not being called

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");
    }

Upvotes: 6

Views: 12551

Answers (4)

Sanjay Sharma
Sanjay Sharma

Reputation: 111

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);
    }
}

Upvotes: 6

Nalan Madheswaran
Nalan Madheswaran

Reputation: 10572

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";
        }

Upvotes: 0

Imaginary
Imaginary

Reputation: 259

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
                    }
                });

Upvotes: 0

Andrei
Andrei

Reputation: 56726

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.

Upvotes: 6

Related Questions