PDA

View Full Version : Not Having Luck Converting responseText to JSON Object


Dean440
02-12-2010, 04:02 PM
Here is the exact string returned as responseText to my XHR Object:

{title:"Hello from the database!", keywords:"hello", description:"A brand new website!", body:"<h1>Website</h1>

<p>Hello from the database!</p>

"}

I've tried using this with no success:

var PageInfo = eval("(" + http.responseText + ")");
alert(PageInfo.title); //Does not alert anything

I've also tried putting "myVar = " at the beginning of the responseText and using just eval(http.responseText) to no avail. Lastly, I've tried this JSON.parse() method I saw somewhere, which didn't work.

So how's this properly done?

rnd me
02-12-2010, 09:37 PM
that's not json, it's javascript. javascript cannot have line breaks inside of string literals.
remove or escape the line breaks and it should work just fine.