本文介绍了Chrome sendrequest错误:TypeError:将循环结构转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几项:

  chrome.extension.sendRequest({
req: getDocument,
docu:pagedoc,
name:'name'
},function(response){
var efjs = response.reply;
});

调用以下内容。

  casegetBrowserForDocumentAttribute:
alert(ZOMG HERE);
sendResponse({
reply:getBrowserForDocumentAttribute(request.docu,request.name)
});
休息;

但是,我的代码从未到达ZOMG HERE,但在运行<$ c时抛出以下错误codeh code $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b chromeHidden.JSON.stringify
chrome.Port.postMessage
chrome.initExtension.chrome.extension.sendRequest
suggestQuery

有没有人知道是什么导致了这种情况?

解决方案

您在请求中传递的对象(我猜它是 pagedoc )有一个循环引用,如下所示:

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

JSON.stringify 无法转换这样的结构。



NB :即使没有附加到DOM树,DOM节点也会遇到循环引用。在大多数情况下,每个节点都有一个 ownerDocument ,它引用文档文档至少通过 document.body document.body引用DOM树.ownerDocument 再次引用文档,它只是DOM树中多个循环引用的一个 p>

I've got the following...

chrome.extension.sendRequest({
  req: "getDocument",
  docu: pagedoc,
  name: 'name'
}, function(response){
  var efjs = response.reply;
});

which calls the following..

case "getBrowserForDocumentAttribute":
  alert("ZOMG HERE");
  sendResponse({
    reply: getBrowserForDocumentAttribute(request.docu,request.name)
  });
  break;

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?

解决方案

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 cannot convert structures like this.

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.

这篇关于Chrome sendrequest错误:TypeError:将循环结构转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:48