Javascript post路由器的req.body包含新行和冒号?
我想知道如何从req.body中提取post数据 我的帖子数据是Javascript post路由器的req.body包含新行和冒号?,javascript,json,node.js,express,Javascript,Json,Node.js,Express,我想知道如何从req.body中提取post数据 我的帖子数据是 { name:'asdf', completed: false, note: 'asdf' } 当我试图使用JSON.stringify来控制台它时,我得到的req.body是 {"{\n name:'asdf',completed:false,note:'asdf'}":""} 我注意到在req.body对象中添加了新行和冒号。因此,当我试图过滤req.body.name时,它返回我未定义 我使用了app.use(bo
{
name:'asdf',
completed: false,
note: 'asdf'
}
当我试图使用JSON.stringify来控制台它时,我得到的req.body是
{"{\n name:'asdf',completed:false,note:'asdf'}":""}
我注意到在req.body对象中添加了新行和冒号。因此,当我试图过滤req.body.name时,它返回我未定义
我使用了app.use(bodyParser.json())代码>但我仍然没有得到实际结果
因此,我想知道以下几点:
1.如何过滤post对象?
2.为什么要在req.body对象中添加新行和冒号?我通过以下方法找到了解决方案 当我在rest客户端测试时,我犯的错误是
name asdf
completed false
note asdf
所有解析器都接受一个类型选项,该选项允许您更改中间件将解析的内容类型
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))
// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))
您是否将文章的标题数据类型设置为JSON?我正在使用AdvanceREST客户端发送请求,我已将内容类型设置为application/JSON,并将数据传递为{'name':'asdf','completed':false',note':'asdf'}之前,我错误地将内容类型设置为application/x-www-form-url-encoded。现在,当我将其设置为application/json时,得到的响应是错误的请求。我遗漏了什么?你能在发布之前对数据进行字符串化吗?我尝试将字符串化为“{'name':'asdf','completed':false,'note':'asdf'}”,但我还是收到了同样糟糕的请求