最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Returning JsonResult results in 500 Internal Server Error - Stack Overflow

programmeradmin1浏览0评论

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

jQuery

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

controller

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.

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.

The WorkFlowAssignment class has many properties and methods.

Can anyone point me in the right direction?

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

jQuery

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

controller

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.

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.

The WorkFlowAssignment class has many properties and methods.

Can anyone point me in the right direction?

Share Improve this question edited Aug 23, 2021 at 18:07 Asef Hossini 7531 gold badge9 silver badges11 bronze badges asked Nov 6, 2011 at 12:28 Derek MitchellDerek Mitchell 931 gold badge1 silver badge3 bronze badges 3
  • 500 Internal Server Error - would suggest that the GetJsonWFA function is failing - have you tried stepping through the function? – ipr101 Commented Nov 6, 2011 at 12:35
  • Fire up Chrome's developer tools and look at the 500 contents when it occurs. That should include a stack trace. – xanadont Commented Nov 6, 2011 at 13:27
  • @ipr101 - the function works fine when stepping through - it seems like the method fails when trying to massage the list to return the JsonResult. – Derek Mitchell Commented Nov 6, 2011 at 16:26
Add a comment  | 

1 Answer 1

Reset to default 15

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

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.

发布评论

评论列表(0)

  1. 暂无评论