[英]express post receive json isn`t I send
I use request to send a json data to my express server. 我使用请求将json数据发送到我的快递服务器。
But express receive the data isn`t my json. 但是快速接收数据不是我的json。
I will show my code about this question. 我将显示有关此问题的代码。
{
'commodityList': [
{
'commodityName': 'aaa',
'commodityId': '2',
'commodityPrice': 128,
'commodityNumber': 2
},
{
'commodityName': '',
'commodityId': '1',
'commodityPrice': 59,
'commodityNumber': 10
}
],
'purchasePrice': 846,
'userId': '1'
}
{ 'commodityList[0][commodityName]': 'aaa',
'commodityList[0][commodityId]': '2',
'commodityList[0][commodityPrice]': '128',
'commodityList[0][commodityNumber]': '2',
'commodityList[1][commodityName]': 'bbb',
'commodityList[1][commodityId]': '1',
'commodityList[1][commodityPrice]': '59',
'commodityList[1][commodityNumber]': '10',
purchasePrice: '846',
userId: '1' }
var request = require('request');
var options = {
url: 'http://localhost:3000/commodityManage/purchaseAdd',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
form: {
'commodityList': [
{
'commodityName': 'aaa',
'commodityId': '2',
'commodityPrice': 128,
'commodityNumber': 2
},
{
'commodityName': 'bbb',
'commodityId': '1',
'commodityPrice': 59,
'commodityNumber': 10
}
],
'purchasePrice': 846,
'userId': '1'
}
};
console.log(options.form.commodityList);
console.log(options.form.commodityList.length);
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log("info:", info);
}
}
request.post(options, callback);
router.post('/purchaseAdd', function(req, res, next) {
var uploadData = req.body;
console.log(uploadData);
}
I didn`t know how to post json what I want. 我不知道如何发布json我想要什么。
Help me,please. 请帮帮我。 (´・_・`) (´・ _ ・`)
My express version is v4 我的快递版本是v4
My node version is v4.2.4 我的节点版本是v4.2.4
Note: 注意:
The json I send 我发送的json
A JavaScript Object initializer isn't JSON, despite the similarities in their syntax. 尽管JavaScript对象初始化器的语法相似,但它们不是JSON。
JSON is a text format for representing structured data. JSON是一种文本格式,用于表示结构化数据。 It however isn't code. 但是,它不是代码。
If you'd like to send a JavaScript Object written out as JSON, you can use request
's json
option : 如果您想发送写为JSON的JavaScript对象,则可以使用request
的json
选项 :
var options = {
url: 'http://localhost:3000/commodityManage/purchaseAdd',
headers: {
// ...
},
json: { // <----
'commodityList': [
{
'commodityName': 'aaa',
'commodityId': '2',
'commodityPrice': 128,
'commodityNumber': 2
},
{
'commodityName': 'bbb',
'commodityId': '1',
'commodityPrice': 59,
'commodityNumber': 10
}
],
'purchasePrice': 846,
'userId': '1'
}
};
This will write out the data in the object as: 这将写出对象中的数据为:
{"commodityList":[{"commodityName":"aaa","commodityId":"2",...
By using the form
option instead, the object is serialized as URL-encoded , formatted for application/x-www-form-urlencoded
: 通过使用form
选项,该对象被序列化为URL编码 ,格式为application/x-www-form-urlencoded
:
// key1=value&key2=value&...
commodityList%5B0%5D%5BcommodityName%5D=aaa&commodityList%5B0%5D%5BcommodityId%5D=2&...
The json your receiving is the json that is being sent. 您接收的json是正在发送的json。 It's just being displayed in a different format. 它只是以其他格式显示。
To prove it try doing something like this: 为了证明它,尝试做这样的事情:
router.post('/purchaseAdd', function(req, res, next) {
//this will transform the object to a string so you can see all the keys/values
var uploadData = JSON.stringify(req.body);
console.log(uploadData);
}
I set the bodyParser in app.js to true 我在app.js中将bodyParser设置为true
like this 像这样
var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: 'lmb'}));
app.use(bodyParser.urlencoded({ extended: true}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.