Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript exception :Uncaught TypeError: Converting circular structure to JSON

I hava JSON object :

[#1={id:"2012-05-04", title:"Scheduled", start:(new Date(1336096800000)), source:{events:[#1#], className:[]}, _id:"2012-05-04", _start:(new Date(1336089600000)), end:null, _end:null, allDay:true, className:[]}]

I try to stringify it :

var test = JSON.stringify(resourceVacation, censor(resourceVacation));

function censor(censor) {
    return (function() {
        var i = 0;
        return function(key, value) {
            if (i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value)
                return '[Circular]';

            ++i; // so we know we aren't using the original object anymore

            return value;
        }
    })(censor);
}

I use censor as mentioned here :Chrome sendrequest error: TypeError: Converting circular structure to JSONn

However I get the following exception over the browser:

Uncaught TypeError: Converting circular structure to JSON

Here is the Java Script object: enter image description here

I got the previous JSON object using toSource() at Mozilla browser. Any idea how to fix it !!

============================UPDATE========================

Actually I need to share with you the scnerio from the beginning: 1 -Initially: I have a form and at the end I build java script object which is :

#1=[{id:"2012-05-03", title:"Scheduled", start:(new Date(1336010400000)), source:{events:#1#, className:[]}, _id:"2012-05-03", _start:(new Date(1336003200000)), end:null, _end:null, allDay:true, className:[]}]

This object is stringified normally ... NOTE THAT IT"S typical to the one that would fire exception later.

2- Then later I delete objects from this array using :

function deleteVacation(day) {
    for (var index = 0; index < resourceVacation.length; index++) {
        if (resourceVacation[index].id == day)

            resourceVacation.splice(index,1);
    }

3-When I try to stringify that array after I deleted a single object , I get the mentioned exception. So .. anu ideas why does it passed the first time and failed 2nd time !!

like image 620
Echo Avatar asked May 08 '12 19:05

Echo


2 Answers

You can't JSON encode date objects.

From json.org : "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested."

like image 160
dontGoPlastic Avatar answered Sep 18 '22 22:09

dontGoPlastic


The problem is the source - object which is a circular reference.

You should create a copy of the object without the source object.

That's how I solved the issue in FullCalendar.

like image 28
user2102131 Avatar answered Sep 21 '22 22:09

user2102131