【发布时间】:2021-03-17 14:32:49
【问题描述】:
我正在尝试在客户端使用 fetch() 将数据发送到我的 nodeJS 服务器或从我的 nodeJS 服务器发送数据。
服务器接收到 post 请求很好,我可以记录 req 变量,但是当我 res.send('any data') 时,客户端无法检测到数据。奇怪的是chrome可以看到响应但我根本不知道如何引用数据!
客户代码
fetch('/',{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user:{
name: 'John',
email: 'J@B.com',
}
})
.then(res => console.log(res))
.then(data => console.log(data))
.catch((error) => console.error('Error:',error))
服务器代码
app.post('/', (req,res) => {
console.log(req.body.user)
res.send('hello?')
})
Chrome able to read response but data field showing undefined
【问题讨论】:
标签: javascript node.js fetch