xmlhttprequest发送json数据使用nodejs接收时一直返回空的问题

javascript 客户端:

  let xmlHttp = new XMLHttpRequest();
  xmlHttp.open("POST", XMLHttpServerUrl+"/requestapi");
  xmlHttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  xmlHttp.onreadystatechange = function(e) {
      if (this.readyState == 4 && this.status == 200) {
          console.log("request succeed.");
      }
  }
  xmlHttp.send(JSON.stringify(category));

nodejs+express+body_parser服务器端:

app.post("/requestapi", bodyParser.json(), function(req, res){
}

第二个参数没有加导致一直无法收到body数据,bodyParser.json()加上后OK,调试了好几个小时,铭记http头类型。

发布了93 篇原创文章 · 获赞 22 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/liushaofang/article/details/79616087