[英]nodejs req.body undefined in case of ajax post requests
奇怪的问题,我对此没有任何解释。 我创建 ajax 发布请求
$.ajax({
type: "POST",
url: "/like",
data: {"topic": "123123"},
success: function(dataString) {
console.log('123123');
}
});
或者像这样:
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/like", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
在服务器端(NodeJs)我有简单的代码
app.post('/like', function (req, res) {
console.log(req.body);
res.status(201).send('+1')
});
我不明白的是,为什么我在发送 ajax 发布请求时总是收到 undefined 。
您需要使用主体解析器,请参阅:nodejs.dev/learn/get-http-request-body-data-using-nodejs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.