[英]How to pass JSON data from server to client side and use the data in Javascript?
I would like to pass a JSON from server side to client side in Node.js.我想在 Node.js 中将 JSON 从服务器端传递到客户端。 On the client side I want to use them in Javascript then.
在客户端,我想在 Javascript 中使用它们。 What I tried so far:
到目前为止我尝试过的:
app.get('/test', function (req, res) {
let data = {
"questions":{
"5-2?":[
{
"ans1":"3"
},
{
"ans2":"8"
},
],
"1+2":[
{
"ans1":"3"
},
{
"ans2":"1"
}
]
}
};
res.render('test', { data: data });
});
In my pug file I want to use that data with Javscript like this:在我的 pug 文件中,我想像这样在 Javscript 中使用这些数据:
console.log(!{data});
On the server you transmit the data using JSON.stringify()
and on the client you receive the serialized JSON representating the data structure that you stringified.在服务器上,您使用
JSON.stringify()
传输数据,而在客户端上,您收到序列化的 JSON,表示您已字符串化的数据结构。 To turn this back in to an object that you can use, use JSON.parse()
要将其重新转换为您可以使用的对象,请使用
JSON.parse()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.