I use request to send a json data to my express server.
But express receive the data isn`t my 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.
Help me,please. (´・_・`)
My express version is v4
My node version is v4.2.4
Note:
The json I send
A JavaScript Object initializer isn't JSON, despite the similarities in their syntax.
JSON is a text format for representing structured data. 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 :
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
:
// 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. 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
like this
var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: 'lmb'}));
app.use(bodyParser.urlencoded({ extended: true}));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.