WebMethod not being called

Seraph812 picture Seraph812 · Aug 31, 2011 · Viewed 10k times · Source

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

Answer

Andrei picture Andrei · Aug 31, 2011

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.