请求正文已声明,但日志显示它在客户端和服务器端均未定义
Request body is declared but the log shows it is undefined both on client-side and server-side
在客户端,我正在执行如下所示的 fetchAPI,我在其中明确声明 req.body
,但是 req.body
被记录为 undefined
,我无法弄清楚原因:
let req=new Request('/register',{
method:'POST',
body:JSON.stringify({
name:'name',
email:'email'
}),
headers:new Headers({
'Content-Type':'application/json'
}),
})
console.log(req.body)
//logs "undefined" on browser console: I wonder why???
fetch(req).then(res=>res.json()).then(data=>{
console.log(data)
})
同样在服务器端,req.body
被记录为未定义:
server.post('/register',(req,res)=>{
console.log(req.body)
// logs "undefined" on Linux console, again I'm not quite sure why!!!
})
更新
正如接受的答案中提到的,缺少 body-parser
中间件,我在服务器端代码中添加了以下代码行:
bodyParser=require('body-parser')
server.use(bodyParser.json())
server.use(bodyParser.urlencoded({extended:false}))
server.post('/register',(req,res)=>{
console.log(req.body)
//Now Linux console logs: "{ name: 'name', email: 'email' }"
//Therefore server receives browser request correctly
res.json({
msg:'Server received your request'
})
})
因此问题现在已解决,Web 浏览器控制台也正确记录了服务器响应:
let req=new Request('/register',{
method:'POST',
body:JSON.stringify({
name:'name',
email:'email'
}),
headers:new Headers({
'Content-Type':'application/json'
}),
})
console.log(req.body)
// Browser console still logs "undefined" here
fetch(req).then(res=>res.json()).then(data=>{
console.log(data)
//Browser console logs here: "Object {msg: "Server received your request"}"
//Therefore browser receives server response correctly
})
唯一不清楚的是网络浏览器仍在将 req.body
记录为 undefined
。根据@jfriend00 的评论,我认为这是因为:
In many frameworks, the body is available in a stream, but has not yet been read when the request starts so there's nothing in req.body yet.
我想在服务器端,您使用的是 Expressjs 框架,对吗?
如果是这样,您需要使用 body-parser 模块从请求负载中解析 json。
代码可能如下所示:
const server = require('express')();
const bodyParser = require('body-parser');
server.use(bodyParser.json());
// Your codes here
server.post()....
在客户端,我正在执行如下所示的 fetchAPI,我在其中明确声明 req.body
,但是 req.body
被记录为 undefined
,我无法弄清楚原因:
let req=new Request('/register',{
method:'POST',
body:JSON.stringify({
name:'name',
email:'email'
}),
headers:new Headers({
'Content-Type':'application/json'
}),
})
console.log(req.body)
//logs "undefined" on browser console: I wonder why???
fetch(req).then(res=>res.json()).then(data=>{
console.log(data)
})
同样在服务器端,req.body
被记录为未定义:
server.post('/register',(req,res)=>{
console.log(req.body)
// logs "undefined" on Linux console, again I'm not quite sure why!!!
})
更新
正如接受的答案中提到的,缺少 body-parser
中间件,我在服务器端代码中添加了以下代码行:
bodyParser=require('body-parser')
server.use(bodyParser.json())
server.use(bodyParser.urlencoded({extended:false}))
server.post('/register',(req,res)=>{
console.log(req.body)
//Now Linux console logs: "{ name: 'name', email: 'email' }"
//Therefore server receives browser request correctly
res.json({
msg:'Server received your request'
})
})
因此问题现在已解决,Web 浏览器控制台也正确记录了服务器响应:
let req=new Request('/register',{
method:'POST',
body:JSON.stringify({
name:'name',
email:'email'
}),
headers:new Headers({
'Content-Type':'application/json'
}),
})
console.log(req.body)
// Browser console still logs "undefined" here
fetch(req).then(res=>res.json()).then(data=>{
console.log(data)
//Browser console logs here: "Object {msg: "Server received your request"}"
//Therefore browser receives server response correctly
})
唯一不清楚的是网络浏览器仍在将 req.body
记录为 undefined
。根据@jfriend00 的评论,我认为这是因为:
In many frameworks, the body is available in a stream, but has not yet been read when the request starts so there's nothing in req.body yet.
我想在服务器端,您使用的是 Expressjs 框架,对吗?
如果是这样,您需要使用 body-parser 模块从请求负载中解析 json。
代码可能如下所示:
const server = require('express')();
const bodyParser = require('body-parser');
server.use(bodyParser.json());
// Your codes here
server.post()....