Ajax-调用 JSON 来自控制器的结果(返回 IList)在 Asp.Net MVC 总是错误
Ajax-Call to get JSON Result from Controller (returning IList) in Asp.Net MVC always errors
我正在尝试进行 AJAX-Call (Javascript) 以获得在控制器方法中创建的 IList
。
index.js:
function getList() {
$.ajax({
url: myUrl,
type: "GET",
dataType: "json",
success: function (response) {
if (response) {
//do sth
}
},
error: function (response) {
alert("error"); //always error
console.log(response); //object with just default functions
}
});
}
MyController.cs:
public IList<SomeItem> loadList()
{
var items = db.SomeItemSet.Include(item => item.Sth).ToList();
IList<SomeItem> resultList = new List<SomeItem>();
for (int i = 0; i < items.Count(); i++)
{
//if(condition)
resultList.Add(items[i]);
}
return resultList;
}
public JsonResult loadListJson()
{
return Json(new { response = loadList() }, JsonRequestBehavior.AllowGet);
}
Controllermethod 中的断点显示它已执行。该列表不为空。
我还尝试将该方法声明为操作结果(return json 没有 jsonreqbehaviour)并在 ajax 中执行类型:POST -称呼。您认为这里有什么可能会失败?
网络调试面板显示代码
302:found
(但不是 200:OK)在尝试加载 ListJson 时。
异常:
System.InvalidOperationException: A circular reference was detected
while serializing an object of type
'System.Data.Entity.DynamicProxies.SomeItem_9D..'. at
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object
o, StringBuilder sb, Int32 depth, Hashtable objectsInUse,
SerializationFormat serializationFormat, MemberInfo currentMember)
at
...
您的对象结构中似乎存在循环引用。JSON 序列化程序不支持它。尝试 Select()
方法。如下所示。
var items = db.SomeItemSet.Include(item => item.Sth).Select(s=> new SomeItemDto{
// which properties you need
}).ToList();
我用 Newtonsoft.Json.JsonConvert 来转换我的列表!
index.js:
function getList() {
$.ajax({
url: myUrl,
type: "POST", //
dataType: "json",
success: function (list) {
if (list) {
//do sth
}
},
error: function (response) { //do sth }
});
}
MyController.cs:
public ActionResult loadListJson()
{
return Content(Newtonsoft.Json.JsonConvert.SerializeObject(loadList()), "application/json");
}
我正在尝试进行 AJAX-Call (Javascript) 以获得在控制器方法中创建的 IList
。
index.js:
function getList() {
$.ajax({
url: myUrl,
type: "GET",
dataType: "json",
success: function (response) {
if (response) {
//do sth
}
},
error: function (response) {
alert("error"); //always error
console.log(response); //object with just default functions
}
});
}
MyController.cs:
public IList<SomeItem> loadList()
{
var items = db.SomeItemSet.Include(item => item.Sth).ToList();
IList<SomeItem> resultList = new List<SomeItem>();
for (int i = 0; i < items.Count(); i++)
{
//if(condition)
resultList.Add(items[i]);
}
return resultList;
}
public JsonResult loadListJson()
{
return Json(new { response = loadList() }, JsonRequestBehavior.AllowGet);
}
Controllermethod 中的断点显示它已执行。该列表不为空。
我还尝试将该方法声明为操作结果(return json 没有 jsonreqbehaviour)并在 ajax 中执行类型:POST -称呼。您认为这里有什么可能会失败?
网络调试面板显示代码
302:found
(但不是 200:OK)在尝试加载 ListJson 时。
异常:
System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.SomeItem_9D..'. at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at ...
您的对象结构中似乎存在循环引用。JSON 序列化程序不支持它。尝试 Select()
方法。如下所示。
var items = db.SomeItemSet.Include(item => item.Sth).Select(s=> new SomeItemDto{
// which properties you need
}).ToList();
我用 Newtonsoft.Json.JsonConvert 来转换我的列表!
index.js:
function getList() {
$.ajax({
url: myUrl,
type: "POST", //
dataType: "json",
success: function (list) {
if (list) {
//do sth
}
},
error: function (response) { //do sth }
});
}
MyController.cs:
public ActionResult loadListJson()
{
return Content(Newtonsoft.Json.JsonConvert.SerializeObject(loadList()), "application/json");
}