Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
    }
like image 572
Seraph812 Avatar asked Aug 31 '11 20:08

Seraph812


People also ask

Does WebMethod need to be static?

As we know, the property of a static method “We can invoke a static method using it's class name without creating an object of this specific class”. This is the reason it's necessary to declare WebMethod as a static method.

What is WebMethod in asp net?

The WebMethod attribute is added to each method we want to expose as a Web Service. ASP.NET makes it possible to map traditional methods to Web Service operations through the System. Web. Services. WebMethod attribute and it supports a number of properties that control the behavior of the methods.


1 Answers

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.

like image 80
Andrei Avatar answered Sep 29 '22 09:09

Andrei