-3

Anybody Please explain what is difference between two codes. i am trying to print JSON String.

1) console.log("Hello "+str);

2)

console.log("Hello");
console.log(str);

and this is working

this code is giving error console.log("Hello "+str); Converting circular structure to JSON

Why it is happening. Can anybody explain me what is difference in both code.

EDIT: this is my code

firstname="hello"; 
lastname= "hhf";
username= "dffflffl";
email="email@hitemai.com
password= "dddd";

var opts = { 
url: 'localhost:8081/register', 
method: 'POST', 
body: JSON.stringify({first_name:firstname,last_name:lastname, user_name:username,email:email,password:password}), 

}; 

Thanks

Hitu Bansal
  • 2,917
  • 10
  • 52
  • 87

2 Answers2

1

JSON cannot handle self-referential objects (cyclic object graphs), so such a thing cannot be printed by the console.

> var str = { x : 1}; str.x = str;
> JSON.stringify(str);

TypeError: cyclic object value
Thilo
  • 257,207
  • 101
  • 511
  • 656
0

If str is supposed to be a JSON string produced by JSON.stringify, then you would get that error if the object you're converting to JSON has a reference to itself or child objects with circular references.

See: Chrome sendrequest error: TypeError: Converting circular structure to JSON.

Community
  • 1
  • 1
jfairbank
  • 151
  • 1
  • 5