【问题标题】:add new data to server using post method not working. using Reactjs and nodejs使用 post 方法向服务器添加新数据不起作用。使用 Reactjs 和 nodejs
【发布时间】:2017-06-02 09:09:33
【问题描述】:

不确定使用 ajax 将数据从客户端发送到服务器的方法。我现在正在使用 Post。下面是我的代码请审查。提前感谢

这是我的客户端代码。

$.ajax(
            {
                url: "/api/addTemplateToDb",
                type: 'POST',
                data:{
                    "Name": tempName.value,
                    "Version": "1.0",
                    "Data": "{"+ tempData.value +"}"
                },
                success: function(result) {
                    console.log(result);
                }.bind(this),
                error:function(result) {
                    console.log(result.responseText);
                }
            }
        );

这是我的服务器端(节点)代码

app.post('/api/addTemplateToDb', function (req, res) {

console.log(req.body.Name); })

我在服务器中得到一个未定义的输出。

【问题讨论】:

    标签: json ajax node.js express


    【解决方案1】:

    在服务器端插入这两行(解析正文)

    var   bodyParser        = require('body-parser');
    app.use(bodyParser.urlencoded({ extended: true })); 

    【讨论】:

      猜你喜欢
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      相关资源
      最近更新 更多