我无法获得列表<>使用Jquery.ajax

时间:2014-09-20 09:26:53

标签: c# jquery asp.net ajax web-services

我希望有人能告诉我下面的代码有什么问题,以及为什么它不起作用。

aspx.cs页面中的webmethod。

    [webmethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public static List<Problem> GetProblems()
    {
        List<Problem> allproblems = new List<Problem>();
        using (TMEntities tm = new TMEntities())
        {
            allproblems = tm.Problems.ToList();    
        }
       return allproblems;
    }

以下是HTML

<script type="text/javascript">
    $(document).ready(function () {
           $.ajax({
                type:"POST",
                url: "WebForm1.aspx/GetProblems",
                data: "{}",
                datatype: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert('success');
                },
                failure: function (response) {
                    alert("fail");
                }
            });
        });
</script>

当我运行应用程序时没有发生任何事情,当我按ctrl + shift + j查看浏览器中的错误时,出现以下错误

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

2 个答案:

答案 0 :(得分:0)

您的TMEntities构造函数是否正在初始化Problems属性? 如果没有,则在下面的行中引用null引用!

allproblems = tm.Problems.ToList();

如果不是上述问题,那么您的Problem类是否有任何序列化问题?检查一次

答案 1 :(得分:0)

[webmethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public static List<Problem> GetProblems()
    {
        List<Problem> allproblems = new List<Problem>();
        using (TMEntities tm = new TMEntities())
        {
            allproblems = tm.Problems.ToList();    
        }
       return allproblems;
    }

我检查你的javascript代码。这段代码还可以。 bt问题是c#代码。

请将[webmethod]更改为[WebMethod]。我希望解决你的问题。如果不解决,请告诉我ProblemTMEntities班级结构。