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头类型。