node.js - 从 React 发送数据到 Node js

标签 node.js ajax reactjs

我正在尝试将数据从 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/

相关文章:

java - 如何在jsp中将服务器目录显示为树形?

java - Servlet 将结果返回到自定义 HTML 页面

facebook - 如何在测试 React.js 应用程序时从 Jest 获得测试覆盖率?

javascript - 在 Material UI Typography 中将内容与变体居中对齐作为标题

node.js - Docker 无法从其他服务连接到 Redis

PHP从网页A触发,从网页B的数据库中加载内容

javascript - 'TypeError : req. flash 不是一个函数' with app.use(flash());但仍然不工作

javascript - React.JS 计算虚拟 DOM 中的 img 宽度

javascript - 将子类 'this' 绑定(bind)到所有父原型(prototype)函数

node.js - Mongoose 聚合 "$sum"子文档中的行