如果我从前端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);
});
答案 0 :(得分:1)
这意味着数据不存在,只需尝试req.body
app.post('/api/login', function(req, res) {
console.log(req.body.username);
console.log(req.body.password);
});