xtra
xtra

Reputation: 69

Javascript eval() not working with passed string

My code snippet:

function receive(mag)
{
    var text = eval(mag);
    alert(text);
}

receive('["magnolia,", ["Magnolia (Flower)"], [], [], [], [], [], [[3, 19019, "INV_MAG_39 ", 5]]]');

When I pass the string in the example (shown above), the eval(mag) doesn't work. But if I do it directly like this:

function receive(mag)
{
    var text = eval('["magnolia,", ["Magnolia (Flower)"], [], [], [], [], [], [[3, 19019, "INV_MAG_39 ", 5]]]');
    alert(text);
}

It does work. Does anyone have an idea whats wrong / how can I get it working with passed variable?

Upvotes: 1

Views: 11746

Answers (1)

frenchie
frenchie

Reputation: 51937

I think you're missing the parenthesis:

eval('(' + mag + ')')

But why not use JSON.parse??

var text = JSON.parse(mag);

Upvotes: 4

Related Questions