我正在尝试通过 HTTP.POST 方法在用户点击发送时以模式形式发布数据,但我无法弄清楚如何实际访问我制作的 post 函数中的数据对象或者如果我发送正确的话。非常感谢任何帮助
index.html 中的 http.post:
var ModalInstanceCtrl = function($scope, $uibModalInstance, $http, data) {
$scope.data = data;
$scope.ok = function() {
$http.post("http://127.0.0.1:8081/process_post")
.then(function (response) {$scope.data = response.data;});
$uibModalInstance.close();
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
};
在app.js中处理post函数:
app.post('/process_post', urlencodedParser, function (req, res) {
// Prepare output in JSON format
//response = req;
//console.log(req.body.data);
//res.end(JSON.stringify(response));
})
最佳答案
您错过了 $http.Post 中的第二个参数。您的数据对象应该作为第二个参数传递到 http.Post 函数中。可以是这样的
$http.Post('http://127.0.0.1:8081/process_post', { data: $scope.data })
如果是nodeJS + expressJS,则可以通过req.body.data访问数据。它应该显示数据。如果 console.log(req.body.data) 没有显示任何内容,请尝试删除中间件 urlencodedParser。
最终更新:
添加这两行以从nodeJS + expressJS获取数据。
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
关于javascript - $http.post 来自模态形式的数据( Angular ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43555043/