到Node.js服务器的Openui5 jQuery POST在服务器端包含空的请求主体

时间:2019-05-11 09:53:40

标签: node.js express

我正在使用Openui5作为前端框架,使用Node.js和Express.js作为后端API以及将MongoDB作为数据库来设置示例应用程序。 我的JQuery Ajax帖子到达后端时不包含任何正文数据。

我尝试了stackoverflow提供的几种解决方案,但是似乎没有一种对我有用。 MongoDB和后端服务器正在运行。数据提取还可以将ui5数据绑定到XML View。

controller.js:

    onSave: function () {
        //get user input from local json model
        var oNewEmployee = this.getView().getModel("newEmp").getProperty("/newEmp"),
            data         = JSON.stringify(oNewEmployee),
            url = 'http://localhost:3000/employee';


        //do the post
        $.ajax({
            url : url,
            dataType : 'json',
            contentType : 'application/json',
            data: data,
            type : 'POST',
            success: function(response){
                console.log(response);
            }
        });
    },

server.js:

var express = require("express");
var app = express();
var mongoose = require("mongoose");
var cors = require("cors");

app.use(cors());

mongoose.connect("mongodb://localhost/schichtplaner", { useNewUrlParser: true });

app.post("/employee", function (req, res) {
    console.log(req.body);
});

app.listen(3000);

我一直未定义为控制台的输出。如果有人知道如何解决这个问题,那就太好了。

1 个答案:

答案 0 :(得分:0)

您应该使用body-parser npm模块才能读取POST请求有效负载。

https://www.npmjs.com/package/body-parser