基本上,我有一个表单,但无法获取输入的数据。(我使用的是 angular 和 node.js)
请求正文
req.params 均为空
我的 html 表单:
<form ng-submit="sendTradelink()">
<md-input-container class="md-accent">
<label>Enter your tradelink</label>
<input ng-model="tradelink">
</md-input-container>
<md-button type="submit" class="md-raised md-accent">Send</md-button>
</form>
Controller :
$scope.sendTradelink = function () {
Auth.send()
.success(function (res) {
$location.path('/');
});
}
服务:
authFactory.send = function (tradelink) {
return $http.post($api.url + 'tradelink', {tradelink: tradelink});
};
服务器端文件,我想在其中处理插入表单的数据:
api.post('/tradelink', function(req, res){
console.log(req.user.id);
console.log(req.params);
console.log(req.body);
res.json({
success: true,
message: 'tradelink received'
})
});
控制日志,每次清空。
最佳答案
如评论中所述,Express 不解析正文。 POST 请求比 GET 请求复杂得多,因此解析器位于一个单独的包中,body-parser
:
npm install --save body-parser
// server.js
api.use(bodyParser.urlencoded({
extended: true
}));
api.post('/tradelink', function(req, res) {
console.log(req.body.tradelink);
});
关于javascript - 无法从 $http.post 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33885216/