javascript 返回 JsonResult 导致 500 Internal Server Error
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8026994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning JsonResult results in 500 Internal Server Error
提问by Derek Mitchell
I'm using jQuery's getJSON function to return a JsonResult from my controller page.
我正在使用 jQuery 的 getJSON 函数从我的控制器页面返回一个 JsonResult。
Here's the jQuery code in the web page:
这是网页中的 jQuery 代码:
$.getJSON("/Test/GetJsonWFA", null, function (data) {
$(data).each(function () {
alert("call succeeded");
//alert(data);
});
And here's the controller code:
这是控制器代码:
public JsonResult GetJsonWFA() {
List<WorkFlowAssignment> listWFAs = new List<WorkFlowAssignment>();
listWFAs.Add(new WorkFlowAssignment() { ID = 1, WorkFlowName = "WorkFlowName1" });
listWFAs.Add(new WorkFlowAssignment() { ID = 2, WorkFlowName = "WorkFlowName2" });
return Json(listWFAs, JsonRequestBehavior.AllowGet);
}
I'm getting the following error: 500 Internal Server Error.
我收到以下错误:500 Internal Server Error。
If I replace the WorkFlowAssignmentin GetJsonWFAwith a trivial class everything works.
如果我更换WorkFlowAssignment在GetJsonWFA用一个简单的课堂上的一切工作。
It seems to be related to the type of object in the list.
它似乎与列表中的对象类型有关。
The WorkFlowAssignmentclass has many properties and methods.
该WorkFlowAssignment类有许多属性和方法。
Can anyone point me in the right direction?
任何人都可以指出我正确的方向吗?
回答by Darin Dimitrov
I suspect that your WorkFlowAssignment
model has some circular references which cannot be JSON serialized. I would recommend you to use a view model and break any possible circular references. Another advantage of using a view model is that you will send to the client only the properties it actually needs in order to do the processing. You don't need to transfer over the wire some complex stuff that the client will never need. So for example if everything that your client needs is the ID
and the WorkFlowName
do this:
我怀疑您的WorkFlowAssignment
模型有一些无法 JSON 序列化的循环引用。我建议您使用视图模型并打破任何可能的循环引用。使用视图模型的另一个优点是您将只向客户端发送它实际需要的属性,以便进行处理。您不需要通过网络传输一些客户端永远不需要的复杂内容。因此,例如,如果您的客户需要的一切都是ID
并且WorkFlowName
执行以下操作:
public ActionResult GetJsonWFA() {
List<WorkFlowAssignment> listWFAs = ...
var viewModel = listWFAs.Select(x => new {
ID = x.ID,
WorkFlowName = x.WorkFlowName
});
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
and on the client:
在客户端:
$.getJSON("/Test/GetJsonWFA", null, function (data) {
$.each(data, function (index, item) {
alert('ID = ' + item.ID + ', WorkFlowName = ' + item.WorkFlowName);
});
});
Also you should use debugging tools such as FireBug or Developer Toolbar to inspect the AJAX request that your browser sends and analyze the server response for eventual errors. When an AJAX request fails your first reaction as a developer should be to launch your debugging tool and see exactly what request/response is being sent.
此外,您应该使用 FireBug 或 Developer Toolbar 等调试工具来检查浏览器发送的 AJAX 请求,并分析服务器响应中的最终错误。当 AJAX 请求失败时,您作为开发人员的第一反应应该是启动调试工具并准确查看正在发送的请求/响应。