General Question

mrentropy's avatar

Javascript 'eval' problem?

Asked by mrentropy (17213points) January 20th, 2011

I’m trying to do something simple in Javascript, which is to receive JSON data and insert that data into some variables. The line that’s giving me a problem reads like this:

myData = eval(’(’ + value.responseText + ’)’);

That, by the way, is exactly the way that ever example I’ve ever seen, be it in books or on the web, is written. Exactly. Even cut & pasted and just changing the variable name. Whenever I use it I always get a really vague error that says:
syntax error
()

Firefox is only slightly helpful in pointing to the end of the line and I’m assuming that this ass-stupid thing is saying that I’m missing a semicolon. Which, clearly, I am not.

The data I’m getting back to the server is correct. That data is:
{“date”:“2011–01-19”,“title”:“Fixes”,“author”:“Me”,“content”:“Bogus test”,“newsid”:“4”}

I found that I was required to create an array using:
var myData = new Array()

And, indeed, if I do this all manually like this:
var myData = new Array({“date”:“2011–01-15”,“title”:“Fixes”,“author”:“Me”,“content”:“Bogus test”,“newsid”:“4”});

Then my code works perfectly. But this:
var myData = new Array(eval(’(’ + value.responseText + ’)’));
gives me the same () error. If I do this:
var myData = new Array(eval(value.responseText));
then I get an error saying myData[0] is undefined.

If somebody could please tell me what’s wrong I would appreciate it. If someone could point me to a resource, somewhere in the world, that is correct I’d appreciate it.

Observing members: 0 Composing members: 0

4 Answers

mrentropy's avatar

Okay, so I decided to join the 21st century and found out about JSON.parse() and figured I’d use that. And…..... it doesn’t work either. Firebug and Firefox stop with the incredibly useful error message:JSON.parse and…. that’s all it says. I mean, really, what the fuck? JSON.stringify() will work, although it doesn’t help my data any so what’s the deal with JSON.parse?

mrentropy's avatar

Okay, well, I fixed it. Evidently just adding and removing random code changes how it works.

ben's avatar

@mrentropy fwiw, eval is generally frowned upon. If you’re using a proper javascirpt framework like jQuery (which you probably should be doing), this server post-processing will be done for you.

mrentropy's avatar

Yeah. I’m not using jQuery. Or any other framework. If I should be using jQuery then jQuery should be part of JavaScript.

No, I was doing it the old fashioned way and building my JavaScript from clay and sticks. The problem turned out to be a global variable that wasn’t actually global. He had aspirations but just never quite made it.

I think what pushed me over the edge was the less than useful error message. Saying “JSON.parse” was slightly less useful than printing out “Error of some kind, maybe?” If it had thrown up something like “JSON.parse argument is NULL” I would have been about six bruises closer to knowing what the problem actually was.

As far as ‘eval’ is concerned, I spent a lot of time thinking that the problem had to do with being passed a semicolon and JS deciding it was getting a line termination (hence the absolutely useless error message of ’)’).

The upside is that now that I went through all the pain, it’s something I’m not going to forget any time soon. And knowing JavaScript means using a framework will be easier once I understand what’s going on.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther