我通过 POST 请求发送以下数组:
let bookings = [{
owner : clientId,
businessId : businessId,
service : serviceId,
startDate : new Date()
}, {
owner : clientId,
businessId : businessId,
service : serviceId,
startDate : new Date()
}];
在服务器上我有以下设置:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
并获取以下req.body
内容:
0[owner]=56fd4dcf8b98ce1c3d87e3ad, 0[businessId]=56fd4dcf8b98ce1c3d87e3ac, 0[service]=56fd4dcf8b98ce1c3d87e3ab, 0[startDate]=2016-03-31T16:18:23.220Z, 1[owner]=56fd4dcf8b98ce1c3d87e3ad, 1[businessId]=56fd4dcf8b98ce1c3d87e3ac, 1[service]=56fd4dcf8b98ce1c3d87e3ab, 1[startDate]=2016-03-31T16:18:23.220Z
如何将其解码回 JavaScript 对象数组?
最佳答案
通过body-parser
使用urlencoded()
功能。
app.use(bodyParser.urlencoded({extended: true}));
extended
The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). The "extended" syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded. For more information, please see the qs library.
Defaults to true, but using the default has been deprecated. Please research into the difference between qs and querystring and choose the appropriate setting.
关于node.js - 如何解码使用 x-www-form-urlencoded 编码的 Javascript 对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338716/