我正在尝试将数据从 React 发送到 Node js。我正在使用 Ajax 。我没有通过 Node.js 获取数据
这是我在 React 上的代码:
让 id = item.id
$.ajax({
url: '/postid',
type: 'POST',
data: id
});
console.log('React id : ' + id);
以及我在 Node.js 上的代码
app.post('/postid', function (req, res) {
console.log("node js ok");
var getId = req.body.id; // here, i don't get the id value
console.log("id Node js : " + getId);
});
你有什么想法吗? 谢谢您
最佳答案
在前端这样做:
$.ajax({
url: '/postid',
type: 'POST',
data: JSON.stringify({ id: 'id here' })
});
在 Node js 服务器上使用 body-parser
否则,您将无法从请求正文中获取参数。
关于node.js - 从 React 发送数据到 Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563683/