Jquery 将json数据从MVC4控制器返回到ajax(不工作)
我将数据从mvc返回到ajax。但是数组没有呈现到ajax成功函数给出了内部服务器错误。我的代码附在下面 我的Ajax调用:Jquery 将json数据从MVC4控制器返回到ajax(不工作),jquery,json,ajax,asp.net-mvc,asp.net-mvc-4,Jquery,Json,Ajax,Asp.net Mvc,Asp.net Mvc 4,我将数据从mvc返回到ajax。但是数组没有呈现到ajax成功函数给出了内部服务器错误。我的代码附在下面 我的Ajax调用: $.ajax({ type: "POST", url: "/Home/getallNews/", success: function (data) { debugger; console.log(data);
$.ajax({
type: "POST",
url: "/Home/getallNews/",
success: function (data) {
debugger;
console.log(data);
alert(data);
}
, function (error) {
alert(JSON.stringify(error));
}
});
和MVC控制器操作:
[HttpPost]
public JsonResult getallNews()
{
//var id = 16;
var Returnmodel = _newsRepository.GetAll().ToList();
return Json(Returnmodel, JsonRequestBehavior.AllowGet);
}
$.ajax({
url: "/Home/getallNews/",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (response) {
alert(response.responseText);
},
success: function (response) {
alert(response);
}
});
});
将ajax调用更改为:
[HttpPost]
public JsonResult getallNews()
{
//var id = 16;
var Returnmodel = _newsRepository.GetAll().ToList();
return Json(Returnmodel, JsonRequestBehavior.AllowGet);
}
$.ajax({
url: "/Home/getallNews/",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (response) {
alert(response.responseText);
},
success: function (response) {
alert(response);
}
});
});
在Ajax调用中添加contenttype和datatype,如下所示
$.ajax({
url: "@Url.Action("getallNews","Home")",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
error: function (response) {
alert(response.responseText);
}
});
使用浏览器工具(网络选项卡)检查响应。错误的详细信息是什么?“/”应用程序中的服务器错误。序列化“System.Data.Entity.DynamicProxies.EntityNews_A56D36119E4D7880562B6207DD29EC42DDA0BAED4A20567E748CC8A346B9E1EE”类型的对象时检测到循环引用。请求URL:请求方法:POST状态代码:500内部服务器错误远程地址:[:1]:31781引用者策略:降级时无引用者请参阅解释。返回仅包含所需属性的匿名对象或视图模型的集合阅读我以前的评论并阅读链接!这两个都不是必需的,这与OP的问题无关。您是否在顶部添加了任何授权属性?这两个都不是必需的,这与OP的问题无关