Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to cache xml DOM of a page in localstorage, I store it in an array and convert it into a json string, but i get this error : "Uncaught TypeError: Converting circular structure to JSON "
how can i do this?

[EDIT]
I load an xml file as follow :
C#
	xml = loadXMLDoc(load_link);
 
//...
function loadXMLDoc(dname){
	if (window.ActiveXObject)
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP.3.0");
	else <br>
		xmlhttp=new XMLHttpRequest();
					
		xmlhttp.open("GET",dname,false);
		xmlhttp.send("");
		return xmlhttp.responseXML;
	}
	//...
	postInfo[5] = xml;
	post[id] = postInfo;
	localStorage["blogInfo"] = JSON.stringify(post); // in this line i get an exeption


Above code comes from OP's comment - Maciej Los
[/EDIT]
Posted
Updated 6-May-14 5:40am
v2
Comments
Sergey Alexandrovich Kryukov 6-May-14 10:43am    
Please show how you do it, indicate the line throwing the exception.
—SA
Coder93 6-May-14 10:57am    
I load an xml file as follow :
xml = loadXMLDoc(load_link);

function loadXMLDoc(dname){
if (window.ActiveXObject)
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP.3.0");
else
xmlhttp=new XMLHttpRequest();

xmlhttp.open("GET",dname,false);
xmlhttp.send("");
return xmlhttp.responseXML;
}
...
postInfo[5] = xml;
post[id] = postInfo;
localStorage["blogInfo"] = JSON.stringify(post); --> in this line i get an exeption

1 solution

Thank you for the clarification.

The exception is unrelated to localStorate; you are trying to use it correctly.

The exception is thrown by JSON.strignify. This type of serialization does not support data graphs with circular references (some technologies, such as .NET Data Contract handle such cases easily). You have some circular reference in your data structure referenced by post. You can review the code which forms this structure.

—SA
 
Share this answer
 
Comments
Coder93 6-May-14 11:18am    
The problem is because of storing DOM in post array which i stringyfy with JSON.
so how can i cache DOM ?
Sergey Alexandrovich Kryukov 6-May-14 11:59am    
No, the problem is the structure of the array (all arrays and other objects in Javascript are associative containers) post. I already answered. There is no problem with storing a string in localStorage.
—SA
Coder93 6-May-14 12:42pm    
when I comment the "postInfo[5] = xml;" I dont get an exception! so other element of an array don't have problem. I think the exception is because of stringify of DOM structure!
Sergey Alexandrovich Kryukov 6-May-14 13:02pm    
You tell me! Isn't that exactly the indication of what I am trying to explain you since some time an out ago?
Read again the answer carefully.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900