简体   繁体   中英

Send json to jade

I am sending a big json file from server to jade, but the " are replaced with: " therefor the json is unreadable/unparsable and I get this error:

Uncaught SyntaxError: Unexpected token &

I send the data like this from node:

res.render(view, {world:{name:"SomeName",width:50},otherdata:{...}});

and then get it in jade like this:

doStuff(JSON.parse(#{data}));

and here it is unreadable data which looks like:

{world:{name:"SomeName",width:50...

can I somehow disable the conversion of the quotes?

No experience with jade but from the language reference ( http://jade-lang.com/reference/interpolation/ ) i guess

doStuff(JSON.parse(!{data}))

might work

Server side within your rout you will do the following consider the object user

var user = {username:"myname"};
res.locals.user = user ;

response will be :

res.render('view');

jade view will have the variable available :

if user
    script(type='text/javascript').
        var user = !{JSON.stringify(user)};

Try adding app.use(bodyParser.json()); if you still have the issue

hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM