Chrome の sendrequest エラー: TypeError: 循環構造を JSON に変換しています 質問する

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

この原因が何なのか分かる人はいますか?

ベストアンサー1

これは、リクエストで渡したオブジェクト ( だと思いますpagedoc) に次のような循環参照があることを意味します。

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

JSON.stringifyこのような構造は変換できません。

注意: これは、DOM ツリーに接続されていない場合でも循環参照を持つ DOM ノードの場合に当てはまります。各ノードには、ほとんどの場合ownerDocumentを参照する があります。は、少なくとも を介して DOM ツリーへの参照を持ち、再びを参照しますが、これはDOM ツリー内の複数の循環参照の1 つにすぎません。documentdocumentdocument.bodydocument.body.ownerDocumentdocument

おすすめ記事