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?

谁能指出我正确的方向?

1 个解决方案

#1


12

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请求失败时,您作为开发人员的第一反应应该是启动调试工具并确切地查看正在发送的请求/响应。

更多相关文章

  1. Angular JS复选框 - 模型不默认为false
  2. three.js学习笔记 obj模型加载问题
  3. 【JavaScript】离线应用与客户端存储
  4. 如何用NodeJS组织构建、服务器、客户端和共享JavaScript代码
  5. 我如何捕获并插入Meteor.Error警报从Meteor.Methods到客户端数据
  6. 如何在Sencha Touch中向模型添加自定义验证规则
  7. 在用户将'n'粘贴复制到文本字段后,如何更新视图模型?
  8. 如何将变量推送到web客户端以获取ajax?
  9. 如何使用客户端Javascript数组并通过节点发布。将js API插入Mong

随机推荐

  1. C语言在屏幕上显示内容
  2. .NET Core2.0小技巧之MemoryCache问题修
  3. .NET CORE如何动态调用泛型解决方法
  4. C#中委托和匿名委托的具体介绍
  5. asp.net mvc如何动态编译生成Controller
  6. C#中关于匿名委托和Lambda表达式的使用详
  7. web.config 配置文件示例详解
  8. ASP.NET Core中的多语言支持的图文详解
  9. C#中关于逆变和协变的详解
  10. MemoryCache问题修复的解决方法