Javascript – How to solve ‘ Uncaught TypeError: Converting circular structure to JSON ‘

extjsjavascript

Im executing a webpage but it displaying following error while checking it on Console.
'Uncaught TypeError: Converting circular structure to JSON'

console.log(JSON.stringify(physicianInfo));
 Ext.Ajax.request(
   { async : true, 
     url : Global.URLPrefix + 'addPhysicianInformation', 
     method : 'GET', 
     params : { 
         physicianInfo : JSON.stringify(physicianInfo), callFrom : 'add' },
     success : function (request, resp) { 
         var physician1 = Ext.decode(request.responseText); console.log(physician1);

Best Answer

This happens when you have a circular reference between objects.

For example:

A references B
B references A

When you try and serialize A then it needs to serialize B, but then it needs to serialize A again and there is no way to represent that loop in JSON. The result would be an infinite recursive loop.

You need to identify and remove the circular references. Remember that the chain can be more complex than the above:

A references B
B references C
C references A

It doesn't matter how many links there are or how they are stored. If you can go from one object to another object and then back to the first one by any route then it is a circular reference.

Related Topic