3 Answers
3

Your problem is that your responseText is in fact a string (responseText = "{'page':'2','endOfPage':'yes','content':'abc'}";). You first have to parse it to create an object, this fails though because you are using single quotes, which are not valid JSON - you'll need to use double quotes for resolving the problem:

Where does the OP display a JSON object as a string? This (OP): {'page':'2','endOfPage':'yes','content':'abc'} does not equal This (yours): "{'page':'2','endOfPage':'yes','content':'abc'}"; You can paste the code in your JS console and it works.
–
HardradaNov 6 '12 at 7:28

Yep, the OPs code works in the console, because responseText is already an object. But because it's probably a reponse from a HTTP-request, I assumed it was in fact a string, and that's why it failed.
–
Lukas_SkywalkerNov 6 '12 at 7:32

yes, same here using eval is the worst mistake i ever did, now trying to fix up things without it.
–
Coder anonymousNov 6 '12 at 7:40

Given that the original responseText was in fact quoted (which your example is not), the only way to eval this is to use surrounding parens to avoid the ambiguity of a block statement. So the correct code would be eval('(' + '{....}' + ')');.
–
Sean KinseyNov 6 '12 at 7:41

1

@SeanKinsey Both parts wrong. One, the JSON comes from an external server according to OP - how would it be trusted? And even if it was, it's still not a good practice to use eval() for which it isn't designed. Furthermore, it doesn't matter what library eval() uses as long as it executes the data it parsed - the security hole is not in the underlying library, but in the fact of the execution itself.
–
user529758Nov 6 '12 at 8:05

1

native JSON.parse is likely to be faster since it can use a more optimized parser.
–
ThiefMaster♦Nov 6 '12 at 8:23