Reputation: 25
I have a problem, I can't pass my variable in my script tag in my pug file.
- var toto = JSON.stringify({"lol":"azd", "lol2":"gdc"});
script.
$(document).ready(function () {
var te = JSON.parse("#{toto}");
console.log(te.lol);
});
Result in html:
$(document).ready(function () {
var te = "{"lol":"azd","lol2":"gdc"}";
console.log(te.lol);
});
Result in console:
Uncaught SyntaxError: Invalid or unexpected token
Thanks for your help !
Panorius.
Upvotes: 0
Views: 90
Reputation: 1141
The reason is that toto
's value is escaped. Use !
to use unescaped value:
- var toto = JSON.stringify({"lol":"azd", "lol2":"gdc"});
script.
$(document).ready(function () {
var te = JSON.parse("!{toto}");
console.log(te.lol);
});
Upvotes: 2