Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize fullcalendar source. Converting circular structure to JSON error

I use fullcalendar jquery plugin from http://arshaw.com/fullcalendar/ and I use knockout js on my website.

I added the event array what is the source of the calendar. The user can modify the events (array).

Then I want to serialize the event array, send by ajax, but I can not, because the calendar modifies my array, and puts an cycle into the source array. How can I remove the changes. Why is there a cycle in my array? I read, may be there is an DOM object in this.

Chrome sendrequest error: TypeError: Converting circular structure to JSON

var a = [];
a.push({
  title: "Event2",
  start: "2013-09-05"
});
a.push({
  title: "Event2",
  start: "2013-09-15"
});

$("#calendar").fullCalendar({
  events: a,
  header: {
    left: "title",
    center: "",
    right: "today prev,next"
  },
  editable: false
});

console.log(JSON.stringify(a));

TypeError: Converting circular structure to JSON

How can I fix it? What is the cause of the cycle?

fiddle example, you can see my problem:

http://jsfiddle.net/erbsaag/XC3NH/1

like image 469
Lajos Avatar asked Nov 12 '22 21:11

Lajos


1 Answers

The plugin is modifying your data.

If you run

console.log(a)

before your console.log you can see the issue. One solution is to only return the fields you need, and not return the fields which have cyclical recursions.

Example:

console.log(JSON.stringify(a.map(function(ai) { return {title: ai.title, start: ai.start}})));
like image 168
Ralph Ritoch Avatar answered Nov 14 '22 23:11

Ralph Ritoch