快递bodyParser失败我的JSON
我用axios发送POST请求给我的nodejs服务器
axios.post(config.SERVER_URL + 'getData', { id: '1234' }) .then(function (response) { console.log(response.data); }) .catch(function (response) { console.log(response); });
数据
{ “ID”: “1234”}
Chromenetworking
bodyParser失败我的对象,所以我收到这样的
{ '"id":"1234"}': '' }
NodeJS代码
app.post('/getData', function(req, res) { res.writeHead(200, { "Content-Type": "application/json" }); console.log(req.body); res.end(JSON.stringify({ data: 'hello'})); });
安慰
试图重新安装bodyParser和expression,并没有帮助。 我这样使用它
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json());
只是不明白为什么会发生。
您似乎正在您的请求中发送form-urlencoded
数据。 尝试设置标题,告诉你正在发送application/json
axios({ method: 'post', url: config.SERVER_URL + 'getData', data: { id: '1234' }, headers: { 'Content-Type': 'application/json' } });