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

发布于 2024-10-10 03:49:15 字数 870 浏览 0 评论 0原文

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

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);

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

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

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无边思念无边月 2024-10-17 03:49:15

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

https://github.com/ljharb/qs

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

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

There is a new node package that fixes this: "npm install 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)"

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.)

暮光沉寂 2024-10-17 03:49:15

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

您可以在 https://github.com/ry 查看 stringify 的源代码/node/blob/master/lib/querystring.js

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

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

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

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

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

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

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.

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

贱人配狗天长地久 2024-10-17 03:49:15

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

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

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

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

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 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.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文