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

json - Javascript: TypeError: cyclic object value - Stack Overflow

programmeradmin2浏览0评论

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

TypeError: cyclic object value

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

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

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

TypeError: cyclic object value

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

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

Share Improve this question asked Sep 1, 2015 at 14:44 Neil PNeil P 3,1907 gold badges41 silver badges74 bronze badges 1
  • can you post the JSON data in newServiceObject object ? – J Santosh Commented Sep 1, 2015 at 14:48
Add a comment  | 

2 Answers 2

Reset to default 9

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

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>

My problem was that when using jquery to build the array, I should have included the toArray() method after the map function.

var list = $(".ServiceCodeName").map(function() { return $(this).html(); } ).toArray();

Therefore when the array is included in the object, it is a standard array and not a jquery object.

发布评论

评论列表(0)

  1. 暂无评论