I am sending POST request to my server via $.ajax and my JSON object is experiencing some problem in server
Client Side code:
var someArr = ["ayush","hehehe"];
var data = {
"profileType": "",
"location": someArr,
"centerPref": 0,
"subjects": []
};
console.log(data.location);
$.ajax({
type: "POST",
url: "/upload/furtherDetails",
data: data
})
But on console logging req.body I get this output
It's because your json data is urlencoded. In order for it to be parsed into a rich json object you have to use a special library to do so.
Here's how to do it with the body-parser package:
const bodyParser = require('body-parser');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }));
Difference it makes:
-- with: bodyParser.urlencoded({ extended: true }) --
{ profileType: '',
location: [ 'ayush', 'hehehe' ],
centerPref: '0' }
-- with: bodyParser.urlencoded({ extended: false }) --
{ profileType: '',
'location[]': [ 'ayush', 'hehehe' ],
centerPref: '0' }
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.