yozawiratama
yozawiratama

Reputation: 4318

asp.net ajax to code behind with parameter

Please help me, why my code is wrong? I am following along from this.

this is my code behind:

public static string HelloName(string name)
{
    return "hello, " + name;
}

this is my jQuery:

$('#Name').click(function () {
    var name = "step";
    //var dataValue = {"name":name};
    $.ajax({
        type: "POST",
        url: "Default.aspx/HelloName",
        data: JSON.stringify({ name: name }),
        contentType: "application/json;charset=utf-8",
        dataType: "JSON",
        success: function (msg) {
            var mes = msg.d;
            console.log(mes);
            $("#Name").text(mes);
        }
    });
});

I think I followed the tutorial correctly, but what I get is an HTTP 500 Internal Error when I use Page Inspector/Network.

please help me where is my false and correct this piece of code.

Upvotes: 3

Views: 2205

Answers (1)

Florian F.
Florian F.

Reputation: 4700

You forgot the attribute [WebMethod] on your HelloName method, so your code should look like this:

[WebMethod]
public static string HelloName(string name)
{
    return "hello, " + name;
}

Upvotes: 5

Related Questions