While using google chrome, I encountered the following scenario:
I had an object in javascript that looked like this:
var b = {
text: 'hello world, you "cool"'
};
I then used JSON.stringify to convert it to a string and sent it to the database. In the database, it appeared as:
{"text":"hello world, you \"cool\""}
Upon retrieving it from the database on the server and attempting to send it back to JavaScript by parsing it, I experienced an issue. Even though it was a valid JSON string, passing it to JavaScript through server scripting language like this caused an error:
"javascript:loadText(' + str + ')"...
The loadText
function is a JavaScript function:
function loadText(val) {
console.log(JSON.parse(val));
}
This resulted in an error. However, when trying another approach in JavaScript alone without encountering the error:
var b = {
text: 'hello world, you "cool"'
};
console.log(JSON.parse(JSON.stringify(b)));
Could this be due to the JSON stringify/parse algorithm?
PS:
This alternative method also proved to be problematic:
loadText('{"text":"hello world, you \"cool\""}');