返回JsonResult结果500内部服务器错误

返回JsonResult结果500内部服务器错误

本文介绍了返回JsonResult结果500内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery的功能的getJSON从我的控制器页面返回JsonResult。

I'm using jQuery's getJSON function to return a JsonResult from my controller page.

下面是在Web页面jQuery的code:

Here's the jQuery code in the web page:

    $.getJSON("/Test/GetJsonWFA", null, function (data) {
        $(data).each(function () {
            alert("call succeeded");
            //alert(data);
        });

和这里的控制器code:

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);

    }

我收到以下错误: 500内部服务器错误

如果我更换 WorkFlowAssignment GetJsonWFA 有一个简单的类一切正常。

If I replace the WorkFlowAssignment in GetJsonWFA with a trivial class everything works.

这似乎是与对象的列表中的类型。

It seems to be related to the type of object in the list.

WorkFlowAssignment 类有许多属性和方法。

The WorkFlowAssignment class has many properties and methods.

任何人都可以点我在正确的方向?

Can anyone point me in the right direction?

推荐答案

我怀疑你的 WorkFlowAssignment 模型具有一定的循环引用,不能是JSON序列。我会建议你使用一个视图模型,并打破任何可能的循环引用。使用视图模型的另一个好处是,你将发送到客户端只有它实际上以做处理所需的性能。你并不需要通过电线一些复杂的东西,客户端不再需要转移。因此,例如,如果您的客户端所需要的一切是 ID WorkFlowName 做到这一点:

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:

public ActionResult GetJsonWFA() {
    List<WorkFlowAssignment> listWFAs = ...

    var viewModel = listWFAs.Select(x => new {
        ID = x.ID,
        WorkFlowName = x.WorkFlowName
    });
    return Json(viewModel, JsonRequestBehavior.AllowGet);
}

和客户端上的:

$.getJSON("/Test/GetJsonWFA", null, function (data) {
    $.each(data, function (index, item) {
        alert('ID = ' + item.ID + ', WorkFlowName = ' + item.WorkFlowName);
    });
});

此外,你应该使用调试工具,如萤火虫或开发工具栏来检查您的浏览器发出AJAX请求并分析最终错误的服务器响应。当一个AJAX请求失败,你作为一个开发者的第一反应应该是启动您的调试工具,看到正在发送什么请求/响应。

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.

这篇关于返回JsonResult结果500内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:01