简体   繁体   English

返回JsonResult会导致500内部服务器错误

[英]Returning JsonResult results in 500 Internal Server Error

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内部服务器错误

If I replace the WorkFlowAssignment in GetJsonWFA with a trivial class everything works. 如果我用一个普通的类替换GetJsonWFA中WorkFlowAssignment一切正常。

It seems to be related to the type of object in the list. 它似乎与列表中的对象类型有关。

The WorkFlowAssignment class has many properties and methods. WorkFlowAssignment类有许多属性和方法。

Can anyone point me in the right direction? 谁能指出我正确的方向?

I suspect that your WorkFlowAssignment model has some circular references which cannot be JSON serialized. 我怀疑你的WorkFlowAssignment模型有一些循环引用,不能进行JSON序列化。 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: 因此,例如,如果客户端需要的所有内容都是IDWorkFlowName请执行以下操作:

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. 此外,您应该使用调试工具(如FireBug或Developer Toolbar)来检查浏览器发送的AJAX请求,并分析服务器响应以查找最终错误。 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. 当AJAX请求失败时,您作为开发人员的第一反应应该是启动调试工具并确切地查看正在发送的请求/响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM