表达req.body无法得到正确的json对象?

时间:2016-04-18 08:37:39

标签: node.js express

我想将json发布到我的node.js应用程序:

{"zhinengList": [{"pos1": 1, "pos2": 2}] }

但是console.log(req.body)得到了这个:

{ zhinengList: [ 'pos2', 'pos1' ] }

1 个答案:

答案 0 :(得分:1)

您可能需要在请求中指定正确的标头。

如果是纯XHR请求:

xhr.setRequestHeader('Content-type', 'application/json');

jQuery

$.ajax({
  //..
  headers: {       
    'Content-Type': 'application/json'
  }
});

通过这种方式,服务器将正确解析它。