Javascript:TypeError:循环对象值 [英] Javascript: TypeError: cyclic object value

查看:66
本文介绍了Javascript:TypeError:循环对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对javascript对象进行字符串化,但是当我这样做时,出现以下错误:

I am trying to stringify a javascript object, however when I do this a get the following error:

TypeError:循环对象值

TypeError: cyclic object value

我不相信我的代码包含任何循环引用(在对象内部未引用newServiceObject),所以我不明白为什么收到此消息.

I don't believe that my code contains any cyclic references (newServiceObject is not referenced inside the object), so I do not understand why I am getting this message.

我想将包含两个属性和一个数组的对象转换为单个字符串.

I want to turn my object that contains two properties and an array into a single string.

var serviceName = $('#newServiceNameBox').val();
        var serviceCodeElemList = $(".ServiceCodeName").map(function() { return $(this).html(); } );
        //create the new service object
        var newServiceObject = {ServiceId:-1, ServiceName: serviceName, ServiceCodes: serviceCodeElemList };

        var appendNewService = '&newService='+JSON.stringify(newServiceObject);

该错误发生在最后一行(JSON.Stringify()),但我不知道为什么!

The error occurs on the last line (the JSON.Stringify() ) but I have no idea why!

推荐答案

这通常是因为您试图序列化具有周期内指向彼此的JavaScript对象.

This is typically because you are trying to serialize a JavaScript object that has properties that point to each other in a cycle.

在您的示例中,newServiceObject.serviceCodeElemList指向其中确实具有循环的jQuery对象:其context属性指向文档对象.文档对象具有指向DOM元素的指针,而DOM元素具有通过ownerDocument属性

In your example, newServiceObject.serviceCodeElemList points to a jQuery object which does have cycles in it: Its context property which points to a document object. A document object has pointers to DOM elements that have pointers back to the document through the ownerDocument property

    var jqueryObj = $('div');
    console.log(jqueryObj.context); // Document object
    console.log(jqueryObj.context.body.firstChild.ownerDocument); // Document object
    

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

这篇关于Javascript:TypeError:循环对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆