Chrome sendrequest错误:TypeError:将循环结构转换为JSON
我有以下...
chrome.extension.sendRequest({
req: "getDocument",
docu: pagedoc,
name: 'name'
}, function(response){
var efjs = response.reply;
});
其中要求以下..
case "getBrowserForDocumentAttribute":
alert("ZOMG HERE");
sendResponse({
reply: getBrowserForDocumentAttribute(request.docu,request.name)
});
break;
但是,我的代码永远不会达到“ZOMG HERE”,而是在运行chrome.extension.sendRequest
引发以下错误
Uncaught TypeError: Converting circular structure to JSON
chromeHidden.JSON.stringify
chrome.Port.postMessage
chrome.initExtension.chrome.extension.sendRequest
suggestQuery
有谁知道这是什么原因造成的?
这意味着你在请求中传递的对象(我猜它是pagedoc
)有一个循环引用,如下所示:
var a = {};
a.b = a;
JSON.stringify
不能像这样转换结构。
注意 :DOM节点就是这种情况,即使它们没有附加到DOM树上,DOM节点也有循环引用。 在大多数情况下,每个节点都有一个ownerDocument
,用于引用document
。 document
至少通过document.body
和document.body.ownerDocument
引用DOM树,并再次引用document
,该document
只是DOM树中多个循环引用中的一个。
按照在Mozilla中JSON文档, JSON.Stringify
具有第二参数censor
可用于过滤/忽略儿童物品在解析树。 但是,也许你可以避免循环引用。
在Node.js中,我们不能。 所以我们可以做这样的事情:
function censor(censor) {
var i = 0;
return function(key, value) {
if(i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value)
return '[Circular]';
if(i >= 29) // seems to be a harded maximum of 30 serialized objects?
return '[Unknown]';
++i; // so we know we aren't using the original object anymore
return value;
}
}
var b = {foo: {bar: null}};
b.foo.bar = b;
console.log("Censoring: ", b);
console.log("Result: ", JSON.stringify(b, censor(b)));
结果:
Censoring: { foo: { bar: [Circular] } }
Result: {"foo":{"bar":"[Circular]"}}
不幸的是,似乎最多有30次迭代,它会自动假定它是循环的。 否则,这应该工作。 我甚至在这里使用了areEquivalent
,但是JSON.Stringify
仍然在30次迭代后抛出异常。 尽管如此,如果你真的需要的话,还是可以在顶层获得一个体面的对象代表。 也许有人可以改善这一点呢? 在Node.js中为一个HTTP请求对象,我得到:
{
"limit": null,
"size": 0,
"chunks": [],
"writable": true,
"readable": false,
"_events": {
"pipe": [null, null],
"error": [null]
},
"before": [null],
"after": [],
"response": {
"output": [],
"outputEncodings": [],
"writable": true,
"_last": false,
"chunkedEncoding": false,
"shouldKeepAlive": true,
"useChunkedEncodingByDefault": true,
"_hasBody": true,
"_trailer": "",
"finished": false,
"socket": {
"_handle": {
"writeQueueSize": 0,
"socket": "[Unknown]",
"onread": "[Unknown]"
},
"_pendingWriteReqs": "[Unknown]",
"_flags": "[Unknown]",
"_connectQueueSize": "[Unknown]",
"destroyed": "[Unknown]",
"bytesRead": "[Unknown]",
"bytesWritten": "[Unknown]",
"allowHalfOpen": "[Unknown]",
"writable": "[Unknown]",
"readable": "[Unknown]",
"server": "[Unknown]",
"ondrain": "[Unknown]",
"_idleTimeout": "[Unknown]",
"_idleNext": "[Unknown]",
"_idlePrev": "[Unknown]",
"_idleStart": "[Unknown]",
"_events": "[Unknown]",
"ondata": "[Unknown]",
"onend": "[Unknown]",
"_httpMessage": "[Unknown]"
},
"connection": "[Unknown]",
"_events": "[Unknown]",
"_headers": "[Unknown]",
"_headerNames": "[Unknown]",
"_pipeCount": "[Unknown]"
},
"headers": "[Unknown]",
"target": "[Unknown]",
"_pipeCount": "[Unknown]",
"method": "[Unknown]",
"url": "[Unknown]",
"query": "[Unknown]",
"ended": "[Unknown]"
}
我在这里创建了一个小的Node.js模块:https://github.com/ericmuyser/stringy随意改进/贡献!
一种方法是从主对象中剥离对象和函数。 并将更简单的表格串联起来
function simpleStringify (object){
var simpleObject = {};
for (var prop in object ){
if (!object.hasOwnProperty(prop)){
continue;
}
if (typeof(object[prop]) == 'object'){
continue;
}
if (typeof(object[prop]) == 'function'){
continue;
}
simpleObject[prop] = object[prop];
}
return JSON.stringify(simpleObject); // returns cleaned up JSON
};
链接地址: http://www.djcxy.com/p/27353.html
上一篇: Chrome sendrequest error: TypeError: Converting circular structure to JSON
下一篇: How to push one object inside another object in AngularJS?