简体   繁体   English

如何将 JSON 数据从服务器传递到客户端并在 Javascript 中使用数据?

[英]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.

相关问题 如何从服务器端加密数据并将其传递给客户端(javascript)并解密和使用 - How to encrypt data from server side and pass it to client side(javascript) and decrypt and use it 如何传递在服务器端生成的json字符串并将其用作MVC客户端javascript中的参数 - How to pass json string generated on server side and use it as aparameter in javascript on client side in MVC 将数据从Javascript代码传递到服务器端 - Pass data to server side from Javascript code 如何使用json数据从客户端发出套接字? - How to use a json data for socket emit from client side? 从客户端到服务器端签名JSON数据 - Signature the JSON data from client-side to server-side 将变量从服务器端控制器传递到客户端,以在客户端JavaScript中使用 - Pass variable from server-side controller to client-side to use in client-side JavaScript 如何使用来自 javascript(客户端)的变量从 node.js(服务器端)检索数据? - How to retrieve data from node.js (server side) using a variable from javascript (client side)? 如何将json对象从jsp页面的jsp部分传递到javascript部分,即从服务器端到客户端? - How to pass a json object from the jsp part of a jsp page to the javascript part i.e from server side to client side? 如何将/ post JSON数组从客户端传递到服务器? - How to pass /post JSON array to the server from the client side? 如何使用Python(带有Websocket的服务器)和JavaScript(客户端)接收JSON数据 - How to receive JSON data with Python (server with websockets) and JavaScript (client-side)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM