node.js 解析请求的 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11994829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:10:49  来源:igfitidea点击:

node.js parse JSON of request

jsonnode.jsrequest

提问by ndrizza

I am sending a credentials JSON object with the following request to node.js:

我正在向 node.js 发送带有以下请求的凭证 JSON 对象:

credentials = new Object();
credentials.username = username;
credentials.password = password;

$.ajax({
    type: 'POST',
    url: 'door.validate',
    data: credentials,
    dataType: 'json',
    complete: function(validationResponse) {
        ...
    }
});

On the server side i would like to load the submitted credentials into a JSON object to use it further on..

在服务器端,我想将提交的凭据加载到 JSON 对象中以进一步使用它......

However, I don't know how to get the JSON out of the req object...

但是,我不知道如何从 req 对象中获取 JSON ...

http.createServer(
    function (req, res) {
         // How do i acess the JSON
         // credentials object here?
    }
).listen(80);

(i have a dispatcher in my function(req, res) passing the req further on to controllers, so i would not like to use the .on('data', ...) function)

(我的函数(req,res)中有一个调度程序将 req 进一步传递给控制器​​,所以我不想使用 .on('data', ...) 函数)

回答by davidbuzatto

At the server side you will receive the jQuery data as request parameters, not JSON. If you send data in JSON format, you will receive JSON and will need to parse it. Something like:

在服务器端,您将收到 jQuery 数据作为请求参数,而不是 JSON。如果您以 JSON 格式发送数据,您将收到 JSON 并需要对其进行解析。就像是:

$.ajax({
    type: 'GET',
    url: 'door.validate',
    data: {
        jsonData: "{ \"foo\": \"bar\", \"foo2\": 3 }"
        // or jsonData: JSON.stringify(credentials)   (newest browsers only)
    },
    dataType: 'json',
    complete: function(validationResponse) {
        ...
    }
});

At server side you will do:

在服务器端,您将执行以下操作:

var url = require( "url" );
var queryString = require( "querystring" );

http.createServer(
    function (req, res) {

        // parses the request url
        var theUrl = url.parse( req.url );

        // gets the query part of the URL and parses it creating an object
        var queryObj = queryString.parse( theUrl.query );

        // queryObj will contain the data of the query as an object
        // and jsonData will be a property of it
        // so, using JSON.parse will parse the jsonData to create an object
        var obj = JSON.parse( queryObj.jsonData );

        // as the object is created, the live below will print "bar"
        console.log( obj.foo );

    }
).listen(80);

Note that this will work with GET. To get the POST data, take a look here: How do you extract POST data in Node.js?

请注意,这将与 GET 一起使用。要获取 POST 数据,请查看此处:如何在 Node.js 中提取 POST 数据?

To serialize your object to JSON and set the value in jsonData, you can use JSON.stringify(credentials)(in newest browsers) or JSON-js. Examples here: Serializing to JSON in jQuery

要将您的对象序列化为 JSON 并在 jsonData 中设置值,您可以使用JSON.stringify(credentials)(在最新浏览器中)或JSON-js。此处的示例:在 jQuery 中序列化为 JSON

回答by jamjam

Console.log the req

Console.log 请求

http.createServer(
    function (req, res) {

    console.log(req); // will output the contents of the req

    }
).listen(80);

The post data will be there somewhere if it was successfully sent.

如果成功发送,post 数据将在某处。