isomorphic fetch post无法读取服务器上的正文

Mar*_*ure 22 post request body-parser

我正在努力解决这个奇怪的问题,我似乎无法解决.我正在使用isomorphic fetch将数据发布到服务器.我将身体作为JSON字符串发送.但是在服务器上,我无法读取正文,它只是一个空对象.

堆栈是:节点,反应.

这是客户端代码:

handleSubmit = (event) => {
    const { dispatch } = this.props;

    fetch('/api/me', {
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: 'xxx'
      })
    })
    .then(response => response.json())
    .then( json => dispatch( login( json ) ))
    .catch( err => console.log(err) )
  }
Run Code Online (Sandbox Code Playgroud)

服务器代码:

var jsonParser = bodyParser.json()
app.post( '/api/me', jsonParser, ( req, res ) => {
  console.log('req', req.body);
})
Run Code Online (Sandbox Code Playgroud)

我试过谷歌搜索问题.但我发现的少数解决方案并没有达到目的.

非常感谢所有帮助.

BR

马丁

//更新//

想通了,这是一个愚蠢的',我忘记了.'header'应为'header'

J_H*_*J_H 1

感谢您更新其中一个参数应该是复数:

  headers: {
    'Accept': ...,
    'Content-Type': ...
  },
Run Code Online (Sandbox Code Playgroud)

您将其附加到问题中。请随意接受此答案,或使用该文本创建新答案并接受它。然后,“未答复”队列将减少一个可供人们偶然发现的悬空条目。