我正在使用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);
我一直未定义为控制台的输出。如果有人知道如何解决这个问题,那就太好了。