Node.js - 使用查询字符串以 GET/POST 方式发送和接收数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4586716/
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 13:47:42  来源:igfitidea点击:

Node.js - Send and receive Array as GET/POST using querystring

node.jsrequest.querystring

提问by Vanwaril

I've got the following code, but it doesn't seem to work:

我有以下代码,但它似乎不起作用:

var post_req = {
    array: [
        [ {
            param1: 'something',
            param2: 123
        } ],
        [ ],
        [ ],
        [ {
            param2: 'something',
            param4: 1234,
            param1: 'hello'
        } ]
    ]
};
var data_send = querystring.stringify(post_req);

var request = client.request('POST', '/', headers);
request.end(data_send);

and

if( req.method == 'POST' ) {
    req.addListener('data', function(chunk)
    {
        POST = querystring.parse(chunk);
        console.log(POST);
    }
}

I end up with 5 sub-arrays, corresponding to the 5 parameters in the objects but with extra '][' characters in their names:

我最终得到 5 个子数组,对应于对象中的 5 个参数,但它们的名称中有额外的 '][' 字符:

{ array: 
   [ { '][param1': 'something' }
   , { '][param2': '123' }
   , { '][param2': 'something' }
   , { '][param4': '1234' }
   , { '][param1': 'hello' }
   ]
}

采纳答案by OCDev

There is a new node package that fixes this: "npm install qs".

有一个新的节点包可以解决这个问题:“npm install qs”。

https://github.com/ljharb/qs

https://github.com/ljharb/qs

"query string parser for node supporting nesting, as it was removed from 0.3.x, so this library provides the previous and commonly desired behaviour (and twice as fast)"

“用于支持嵌套的节点的查询字符串解析器,因为它已从 0.3.x 中删除,因此该库提供了以前和通常所需的行为(并且速度是原来的两倍)”

If anyone can tell me why it was removed from 0.3.x, I will give you an upvote for your comment. (I want my confidence in Node.js restored.)

如果有人能告诉我为什么从 0.3.x 中删除它,我会为您的评论点赞。(我希望恢复对 Node.js 的信心。)

回答by RandomEtc

To confirm my comment above, node's querystring.stringifyfunction won't handle nested arrays (at the time of writing).

为了确认我上面的评论,节点的querystring.stringify函数不会处理嵌套数组(在撰写本文时)。

You can see the source of stringify at https://github.com/ry/node/blob/master/lib/querystring.js

你可以在https://github.com/ry/node/blob/master/lib/querystring.js看到 stringify 的来源

Note that it handles one level of arrays but it doesn't recurse. When it finds an array it uses stringifyPrimitiveto encode the array's values. You can see that stringifyPrimitivedoesn't handle arrays, only number, boolean and string.

请注意,它处理一层数组,但不会递归。当它找到一个数组时,它stringifyPrimitive用来对数组的值进行编码。你可以看到它stringifyPrimitive不处理数组,只处理数字、布尔值和字符串。

As I suggested in my comment, given that you're using a POST request a better idea would be to use JSON encoding for your complex data structure.

正如我在评论中所建议的,鉴于您正在使用 POST 请求,更好的主意是对复杂的数据结构使用 JSON 编码。

Or use https://github.com/visionmedia/node-querystringas suggested by @FriendlyDev

或者按照@FriendlyDev 的建议使用https://github.com/visionmedia/node-querystring

回答by Edgar Chavolla

Don't use querystring.parse for recreating a JSON object sent as string. Use instead JSON.parse. And use JSON.stringify instead of querystring.stringify

不要使用 querystring.parse 来重新创建作为字符串发送的 JSON 对象。改用 JSON.parse。并使用 JSON.stringify 而不是 querystring.stringify

querystring is useful when you send parameter encoded in the url or when you post a form. But there is no point in using it if you send just one JSON object with all your data.

当您发送在 url 中编码的参数或发布表单时,查询字符串很有用。但是如果您只发送一个包含所有数据的 JSON 对象,那么使用它是没有意义的。

In your scenario I would dismiss the querystring library and use JSON library, which is already included. It would be cleaner and faster.

在您的场景中,我将关闭查询字符串库并使用已包含的 JSON 库。它会更干净、更快。

http://www.json.org/js.html

http://www.json.org/js.html