jQuery “在序列化‘System.Reflection.RuntimeModule’类型的对象时检测到循环引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31008219/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:46:20  来源:igfitidea点击:

"A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'"

jqueryjsonasp.net-mvcasp.net-mvc-4asp.net-ajax

提问by Anand

I am trying to return the object of MVC Model in JSON result using jQuery. I am getting a failure message as:

我正在尝试使用 jQuery 在 JSON 结果中返回 MVC 模型的对象。我收到一条失败消息:

A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'

序列化“System.Reflection.RuntimeModule”类型的对象时检测到循环引用

This is my controller where i am returing the Json result

这是我的控制器,我在那里返回 Json 结果

public ActionResult populateData(string application, string columns, string machine, string pages, string startDate, string endDate)
    {

        ErrorPage _objError = new ErrorPage();
        _objError.ErrorData = dbl.GetDataTable(DbConnectionString, Table, whereCondition, columns);


        //Column description: Name and Type    
        var columnlist = new Dictionary<string, System.Type>();
        foreach (System.Data.DataColumn column in _objError.ErrorData.Columns)
        {       
            var t = System.Type.GetType( column.DataType.FullName );
            columnlist.Add(column.ColumnName, t);  
        }

        _objError.ErrorColumns = columnlist;


        //DataSourceRequest result = _objError.ToDataSourceResult(request);

        if (_objError.ErrorData.Rows.Count > 0)
            Message = "Showing Error log for " + AppName + " . To Change the application or filtering options please select the appropriate application from Application Dropdown";
        else
            Message = "No errors found for " + AppName + " in last 24 hours.";


        return Json(_objError);
    }

Here i am giving a Ajax call to Controller method:

在这里,我对 Controller 方法进行了 Ajax 调用:

$.ajax({
        type: "POST",
        url: '@Url.Content("~/Common/PopulateData")',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: JSON.stringify({ application: app, columns: columns, machine: machine, pages: pages, startDate: startDate, endDate: endDate }),
        success: function (data) {
            alert("Success");
        },
        error: function (error) {
            alert('error; ' + eval(error));
            alert('error; ' + error.responseText);
        }
    });

Kindly help how to return a Model class object to Ajax post call ?

请帮助如何将模型类对象返回到 Ajax 后调用?

回答by Anand

Here we go for Solution

在这里,我们去解决方案

I modified my code with below set of code and it worked for me

我用下面的代码集修改了我的代码,它对我有用

public JsonResult populateData(string application, string columns, string machine, string pages, string startDate, string endDate)
    {
        ErrorPage _objError = new ErrorPage();
        var ErrorResult = _objError.GetErrorData(application, columns, machine, pages, startDate, endDate);


        var result = JsonConvert.SerializeObject(ErrorResult.ErrorData, Formatting.Indented,
                       new JsonSerializerSettings
                       {
                           ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                       });

        return Json(result, JsonRequestBehavior.AllowGet);
    }

We need to serailize the object rather than sending a direct object of Model.

我们需要serailize对象而不是直接发送Model的对象。

Thanks.

谢谢。

回答by Kevin Heidt

A circular reference happens when a property of an object has a property of its own that points back to the parent. It causes an infinite loop. Without seeing the details of what makes up the class ErrorPage, it'll be tough to tell you which property is responsible for this.

当对象的属性具有指向父对象的自身属性时,就会发生循环引用。它会导致无限循环。如果没有看到构成类 ErrorPage 的详细信息,很难告诉您哪个属性对此负责。

But typical solutions for this kind of thing are to either make a ViewModel which is identical to your class structure minus the circular references, or you could use https://json.codeplex.com/which has some decoration attributes you can add to have a property ignored during serialization.

但是这种事情的典型解决方案是制作一个与您的类结构相同的 ViewModel 减去循环引用,或者您可以使用https://json.codeplex.com/它具有一些您可以添加的装饰属性序列化期间忽略的属性。

回答by The_Black_Smurf

The error message is very explicit: The object you're tying to serialize (_objError) has a circular reference. This mean the one of it's properties is pointing to it's own instance directly or indirectly. Ex. instance A has a property pointing to instance B which points to instance A (like a parent property).

错误消息非常明确:您要序列化的对象 (_objError) 具有循环引用。这意味着它的一个属性直接或间接指向它自己的实例。前任。实例 A 有一个指向实例 B 的属性,而实例 B 又指向实例 A(如父属性)。

This is causing the serialization to fail because it would create an infinite loop (A.child = B / B.parent = A / A.child = B / ...). In order to fix this, you'll have to break the circular reference by ignoring the property causing the circular reference or by creating an other object that doesn't has such property.

这会导致序列化失败,因为它会创建一个无限循环(A.child = B / B.parent = A / A.child = B / ...)。为了解决这个问题,您必须通过忽略导致循环引用的属性或创建一个不具有此类属性的其他对象来中断循环引用。