使用c#函数时jquery ajax错误

我想在我的项目中使用jquery ajax。 我只是运行一个带有代码的简单ajax。 我收到警告错误“500内部错误”

  $(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); }  

我的代码背后:

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

对于aspx页面,您必须使用[WebMethod()]属性修饰代码隐藏方法:

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

编辑: WebMethod位于System.Web.Services.WebService命名空间内。