未捕获的TypeError:将循环结构转换为JSON [英] Uncaught TypeError: Converting circular structure to JSON
问题描述
我有一个tableDnD拖放与JSON.stringify:
I have a tableDnD drag and drop with JSON.stringify :
jQuery(document).ready(function() {
jQuery("#Table").tableDnD({
onDragClass: "danger",
onDrop: function(table, row) {
jQuery.ajax({
url: "ajax.php",
type: "post",
data: {
'rows' : JSON.stringify(table.tBodies[0].rows)
},
dataType: 'html',
success: function(reponse) {
if(reponse) {
//alert('Success');
} else {
alert('Erreur');
}
}
});
}
});
});
我有以下错误消息:
Uncaught TypeError:将循环结构转换为JSON
Uncaught TypeError: Converting circular structure to JSON
我的问题仅出在Chrome上。
I have the problem only on Chrome.
推荐答案
你不应该直接将DOM元素转换为JSON。
You should not convert a DOM element to JSON directly.
while - like你已经经历过 - 它失败了,例如在Chrome中,结果可能也会出乎意料。
While - like you already experienced - it fails e.g. in Chrome, the results may also be unexpected.
原因是因为数据是循环的:
The reason for this is because the data is circular:
Node具有属性 childNode
,其中包含其所有子项以及指向父项的属性 parentNode
。
A Node has the property childNode
containing all its children and the property parentNode
pointing to the parent.
JSON格式不支持引用,因此它需要遵循属性直到达到结束,但是因为子项指向其父项,其中包含其子项列表,是一个无限循环,这就是你得到错误的原因:
The JSON format does not support references, so it will need to follow the properties until an end is reached, but because a child points to its parent which has a list of its children, this is an endless loop, that’s the reason why you get the error:
Uncaught TypeError:将循环结构转换为JSON
Uncaught TypeError: Converting circular structure to JSON
即使浏览器已解决此问题,您也可能遇到其他问题。
因为不仅 childNodes
存在,而且 childElements
。同样适用于 parentNode
/ parentElement
,那么你也有 nextSibling
, prevSibling
, firstChild
, lastChild
,..那可能也会被遵循,所以你最终会进入包含重复数据的可怕的大型JSON文件。
Even if this is resolved by the browser you may have other problems.
Because not only childNodes
exist but also childElements
. The same is for parentNode
/parentElement
, then you also have nextSibling
, prevSibling
, firstChild
, lastChild
, ... that would probably also be followed, so you would end up in the terrifying large JSON file containing a butch of duplicate data.
这篇关于未捕获的TypeError:将循环结构转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!