0

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 ";
    }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You'll have to include the System.Web.Services.WebService namespace. I've updated my answer as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.