我无法获得列表<>;使用Jquery.ajax
本文关键字:gt 使用 Jquery ajax lt 列表 | 更新日期: 2023-09-27 18:23:46
我希望有人告诉我下面的代码出了什么问题,以及为什么它不起作用。
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)
TMEntities构造函数是否初始化Problems
属性?如果不是,则在下面的行中出现null引用异常!
allproblems = tm.Problems.ToList();
如果不是上述问题,那么您的Problem
类是否存在任何序列化问题?检查一次
[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]
。我希望能解决你的问题。如果不能解决,请给我你的Problem
和TMEntities
类结构。