-1

I have a function working fine:

function sendLeadData(form) {
    return trk(form, {
        firstName: "pan",
    });
}

{firstName : "Pan"} = an object. If I set this to a var and pass the var in function that also works fine. But I need to use a string which I am building from some map and pass that. I am getting the string perfect .Code below not working:

function sendLeadData(form) {
    //code to get str
    alert("str is "+str);------->prints str perfect,also giving result below
    var obj = JSON.parse(str);//If except str I put JSON.parse(JSON.stringify({firstName:"Pan"})) that work fine
    alert("obj is "+obj);

    return trk(form,
        obj
    );
}

str prints "{firstName:"Pan"}". Error is syntax error thrown.

Please help.

1
  • You need to escape your quotes ("{firstName: \"Pan\"}") or use alternate quotes ('{firstName: "Pan"}'). Commented Nov 19, 2014 at 13:13

2 Answers 2

4

JSON has more strict object representation. The keys also need to be quoted:

'{"firstName": "Pan"}'
0
0

    var obj = {x: 5, y: 6, apple:0, name:'myname'};
    var objToText = JSON.stringify(obj);
    console.log(objToText);
    
    var text = '{"x": 5, "y": 6, "apple":0, "name":"myname"}';
    var textToObj = JSON.parse(text);
    console.log(textToObj);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.