【问题标题】:jquery ajax error while using c# function使用c#函数时出现jquery ajax错误
【发布时间】:2012-05-25 12:02:14
【问题描述】:

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

<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>

我的代码:

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

【问题讨论】:

    标签: c# jquery ajax


    【解决方案1】:

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

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

    编辑: WebMethodSystem.Web.Services.WebService 命名空间内。

    【讨论】:

    • 您必须包含 System.Web.Services.WebService 命名空间。我也更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2013-01-17
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多