表达 bodyParser 失败我 json
express bodyParser fail my json
我用 axios 向我的 nodejs 服务器发送 POST 请求
axios.post(config.SERVER_URL + 'getData',
{
id: '1234'
})
.then(function (response) {
console.log(response.data);
})
.catch(function (response) {
console.log(response);
});
数据
{"id":"1234"}
Chrome 网络
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 和 express,但没有帮助。
我是这样用的
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
就是不明白为什么会这样。
您似乎在请求中发送 form-urlencoded
数据。尝试设置 headers 以告诉快递您正在发送 application/json
axios({
method: 'post',
url: config.SERVER_URL + 'getData',
data: { id: '1234' },
headers: {
'Content-Type': 'application/json'
}
});
我用 axios 向我的 nodejs 服务器发送 POST 请求
axios.post(config.SERVER_URL + 'getData',
{
id: '1234'
})
.then(function (response) {
console.log(response.data);
})
.catch(function (response) {
console.log(response);
});
数据
{"id":"1234"}
Chrome 网络
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 和 express,但没有帮助。 我是这样用的
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
就是不明白为什么会这样。
您似乎在请求中发送 form-urlencoded
数据。尝试设置 headers 以告诉快递您正在发送 application/json
axios({
method: 'post',
url: config.SERVER_URL + 'getData',
data: { id: '1234' },
headers: {
'Content-Type': 'application/json'
}
});