[英]Decode JSON object in req where content type 'application/x-www-form-urlencoded'
[英]How to decode array of Javascript object encoded with x-www-form-urlencoded?
我用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}));
扩展的
扩展选项允许您在使用querystring库(如果为false)或qs库(为true)解析URL编码的数据之间进行选择。 “扩展”语法允许将丰富的对象和数组编码为URL编码格式,从而使URL编码具有类似JSON的体验。 有关更多信息,请参见qs库。
默认值为true,但是不建议使用默认值。 请研究qs和querystring之间的区别,然后选择适当的设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.