How to Pass an Object to client Javascript - NodeJS & express

半腔热情 提交于 2019-11-28 13:06:34

You can send your object as a string.

Like locations = JSON.stringify({local:"my_local",desc:"myDesc"});

And on client side you can parse it to get an Object

Like loc = JSON.parse(locations);

Now you can use loc as Object.

I know this is an old post but in case others are looking for an easy way to pass an object from the server to the client in nodejs here's how I do it.

On the server I do something like:

res.render('my_page',{'my_object': my_object});

Then on the client side in my jade I'd do:

-var name = my_object.name;

(assuming there's a "name" property in my_object)

Then just use it however you need it.

Like:

p Hello #{name}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!