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

However, my code never reaches "ZOMG HERE" but rather throws the following error while running chrome.extension.sendRequest

 Uncaught TypeError: Converting circular structure to JSON
 chromeHidden.JSON.stringify
 chrome.Port.postMessage
 chrome.initExtension.chrome.extension.sendRequest
 suggestQuery

有人知道是什么原因造成的吗?

Does anyone have any idea what is causing this?

推荐答案

表示你在请求中传入的对象(我猜是pagedoc)有循环引用,类似于:

It means that the object you pass in the request (I guess it is pagedoc) has a circular reference, something like:

var a = {};
a.b = a;

JSON.stringify 不能像这样转换结构.

JSON.stringify cannot convert structures like this.

注意:DOM 节点就是这种情况,它们具有循环引用,即使它们没有附加到 DOM 树.每个节点都有一个 ownerDocument,它在大多数情况下引用 document.document 至少通过 document.body 引用 DOM 树,而 document.body.ownerDocument 引用回 document 再次,这只是 DOM 树中多个循环引用中的 一个.

N.B.: This would be the case with DOM nodes, which have circular references, even if they are not attached to the DOM tree. Each node has an ownerDocument which refers to document in most cases. document has a reference to the DOM tree at least through document.body and document.body.ownerDocument refers back to document again, which is only one of multiple circular references in the DOM tree.

相关文章