如果我从前端 angular.js 向后端 node.js 发送一个 JSON,它会给我一个错误:TypeError: Cannot read property username of undefined
。我也在使用 Express。
前端 (Angular.js):
var username_data = $scope.LoginUsername;
var password_data = $scope.LoginPassword;
var data = {username : username_data, password : password_data};
$http.post('/api/login', data)
.then(function (res) {
//to do...
});
带有 Restful API 的后端 (node.js):
app.post('/api/login', function(req, res) {
console.log(req.data.username);
console.log(req.data.password);
});
最佳答案
表示数据不存在,试试req.body代替
app.post('/api/login', function(req, res) {
console.log(req.body.username);
console.log(req.body.password);
});
关于javascript - 将 JSON 从 angular.js 发送到 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48238910/