Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome sendrequest error: TypeError: Converting circular structure to JSON

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?

like image 637
Skizit Avatar asked Jan 27 '11 12:01

Skizit


People also ask

What is TypeError converting circular structure to JSON?

The "Converting circular structure to JSON" error occurs when we pass an object that contains circular references to the JSON. stringify() method. To solve the error, make sure to remove any circular references before converting the object to JSON.

How do you convert circular structure to string?

A circular structure is an object that references itself. To be able to stringify such objects, developers can utilize the replacer parameter in the stringify() method, making sure the function that is being passed in, filters out repeated or circular data.

Is JSON Stringify safe?

stringify() is fine. Using the output of JSON. stringify() in a JavaScript context will result in the expected behavior.


1 Answers

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.

like image 126
Felix Kling Avatar answered Oct 21 '22 09:10

Felix Kling