简体   繁体   中英

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. On the client side I want to use them in Javascript then. 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:

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. To turn this back in to an object that you can use, use JSON.parse()

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