jeni
jeni

Reputation: 983

jquery ajax error while using c# function

I want to use jquery ajax in my project. I just run a simple ajax with code behind. I got the alert error as "500 internal error"

<script type="text/javascript" src="jquery-1.2.6.min.js"></script> 
<script type="text/javascript">

     $(document).ready(function () {

         $.ajax({
             type: "POST",
             url: "listprac.aspx/sayHello",
             contentType: "application/json; charset=utf-8",
             data: "{}",
             dataType: "json",
             success: AjaxSucceeded,
             error: AjaxFailed
         });
     });
     function AjaxSucceeded(result) {
         alert(result.d);
     }
     function AjaxFailed(result) {
         alert(result.status + ' ' + result.statusText);
     }  

  </script>

My code behind:

 public static string sayHello()
    {
        return "hello ";
    }

Upvotes: 0

Views: 386

Answers (1)

Sang Suantak
Sang Suantak

Reputation: 5265

For aspx pages you'll have to decorate your code-behind method with the [WebMethod()] attribute:

[WebMethod()]
public static string sayHello()
{
    return "hello ";
}

Edit: WebMethod is within the System.Web.Services.WebService namespace.

Upvotes: 1

Related Questions