我是一个有点问题的初学者。我似乎无法将表单中的文本传递到后续的req.body中,而这正是我应该使用body解析器得到的。关于这个问题有很多问题,但到目前为止似乎没有任何帮助。请指引我朝正确的方向走。MODS请不要锁定。我已经搜索过了,可以找到一个合适的答案。非常感谢。
快递:
app.use(bodyParser.urlencoded({'extended' : 'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({type: 'application/vnd.api+json'}));
app.use(methodOverride());
app.post('/', function(req, res){
console.log(req.body.text);
res.redirect('https://google.com');
res.end();
角度:
angular.module('application', [])
.controller('mainController', function($scope, $http) {
$scope.formData = {};
$scope.submitForm = function() {
$http.post('/', $scope.formData)
.success(function(data) {
console.log(data);
});
};
}
HTML:
<form method="post" action="/">
<div class="form-group">
<input type="text" class="form-control input-lg text-center" placeholder="email" ng-model="formData.text">
</div>
<button type="submit" class="btn btn-primary btn-lg" ng-submit="submitForm()">Add</button>
</form>
您显然没有向服务器发送对象text
,而是发送了一个包含text
属性的formData
对象。
你可以试试:
console.log(req.body.formData.text);
然后,如果你想检索angular内部的一些数据,你必须将数据发回:
app.post('/', function(req, res){
console.log(req.body.formData.text);
res.send("blablabla");
或
res.json({response: 'response'});