1

How to call a non static void function from ajax.I am getting a error. This is ajax code:-

    $('#button2 button').click(function () {

              $.ajax({
                type: "POST",
                url: "practiced_final.aspx/display",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                success: function () 
                {
                },
                error: function (a, b, c) {
                    alert(a + b + c);
                }
            })
                                return false;

           });

This is C# method code:

      [WebMethod]
 protected  void display()
    {

    HttpContext.Current.Response.Write( "Hello");
    }

This is error message:-

[object XMLHttpRequest]errorundefined

What i am missing?

Please help.

Thank you.

user1627138
  • 213
  • 1
  • 7
  • 17
  • add attribute to your method `display` `[Webmethod]`. I'm not sure if this method of yours should be public or not – Harry89pl Sep 05 '12 at 10:29
  • i had already added it but didn't paste here.i still didn't work. check updated code. – user1627138 Sep 05 '12 at 10:32
  • see this [http://stackoverflow.com/questions/1360253/call-non-static-method-in-server-sideaspx-cs-from-client-side-use-javascript] related question which should solve your question – Pilgerstorfer Franz Sep 05 '12 at 10:36

1 Answers1

2

for it to work your function must be static, it should look like this:

[Webmethod]
public static void display()
{
     HttpContext.Current.Response.Write( "Hello");
}

if you want your function to return a string you should change it to:

[Webmethod]
    public static string display()
    {
         return "Hello";
    }
kleinohad
  • 5,800
  • 2
  • 26
  • 34