JavaScript 异常:未捕获的类型错误:将循环结构转换为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10504945/
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
JavaScript exception :Uncaught TypeError: Converting circular structure to JSON
提问by Echo
I hava JSON object :
我有 JSON 对象:
[#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
我使用这里提到的员:Chrome sendrequest error: TypeError: Converting circle structure to JSONn
However I get the following exception over the browser:
但是,我通过浏览器收到以下异常:
Uncaught TypeError: Converting circular structure to JSON
未捕获的类型错误:将圆形结构转换为 JSON
Here is the Java Script object:
这是 Java Script 对象:
I got the previous JSON object using toSource() at Mozilla browser. Any idea how to fix it !!
我在 Mozilla 浏览器上使用 toSource() 获得了以前的 JSON 对象。任何想法如何解决它!
============================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 :
实际上,我需要从一开始就与您分享 scnerio: 1 -最初:我有一个表单,最后我构建了 java 脚本对象,它是:
#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 :
2-然后我使用以下方法从此数组中删除对象:
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 !!
3-当我在删除单个对象后尝试对该数组进行字符串化时,出现上述异常。所以..anu想法为什么它第一次通过而第二次失败!
回答by dontGoPlastic
回答by user2102131
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.
这就是我在 FullCalendar 中解决问题的方法。