Hard Tour Vela
Hard Tour Vela

Reputation: 198

Unknown web method parameter method name

I'm building a Web Application in which I'm trying to call a WebMethod in a WebForm, I've tried every single page in google but I'm still getting nothing. This is an example of the Jquery Ajax Call

$.ajax({
            type: "Post",
            url: "Default.aspx/Return",
            data: {dato:'Hello'},
            contentType: "application/json; chartset:utf-8",
            dataType: "json",
            success:
                    function (result) {
                        if (result.d) {
                            alert(result.d);
                        }
                    },
            error:
                function (XmlHttpError, error, description) {
                    $("#grdEmpleados").html(XmlHttpError.responseText);
                },
            async: true
        });

And this is the WebMethod in the codebehind

[WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public static string Return(string dato)
    {
        return dato;
    }

Upvotes: 0

Views: 10069

Answers (4)

Scott Sarver
Scott Sarver

Reputation: 23

You can't access a Static method this way. Remove the "Static" reference and it will work. Also, like someone else said - do not use that as your method name "Return".

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string Return(string dato)
{
    return dato;
}

Upvotes: 2

wonde
wonde

Reputation: 684

Try this

var url = window.location.pathname + "/Return";
$.ajax({
        type: "Post",
        url: url,
        data: {dato:'Hello'},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success:
                function (result) {

                        alert(result.d);

                },
        error:
            function (XmlHttpError, error, description) {
                $("#grdEmpleados").html(XmlHttpError.responseText);
            },
        async: true
    });`

Upvotes: 0

Joan Caron
Joan Caron

Reputation: 1970

Make sure that you have enabled page methods in your ScriptManager element:

<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true" />

and your method

$.ajax({
            type: "Post",
            url: '<%= ResolveUrl("~/Default.aspx/Return") %>',
            data: {dato:'Hello'},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success:
                    function (result) {

                            alert(result);

                    },
            error:
                function (XmlHttpError, error, description) {
                    $("#grdEmpleados").html(XmlHttpError.responseText);
                },
            async: true
        });

Upvotes: 0

serene
serene

Reputation: 685

I think, on your success event the function with result is used, which is a string and u are trying to access property named d assuming result is an object. use of only alert(result); User F12 tool to debug and find your error.

Upvotes: 0

Related Questions