Javascript $http.post来自模态表单的数据(角度)

Javascript $http.post来自模态表单的数据(角度),javascript,angularjs,http,post,Javascript,Angularjs,Http,Post,我试图通过HTTP.post方法在点击send的用户上发布模式形式的数据,但我很难弄清楚如何在我创建的post函数中实际访问数据对象,或者我甚至无法正确发送数据对象。非常感谢您的帮助 http.post in index.html: var ModalInstanceCtrl = function($scope, $uibModalInstance, $http, data) { $scope.data = data; $scope.ok = function() {

我试图通过HTTP.post方法在点击send的用户上发布模式形式的数据,但我很难弄清楚如何在我创建的post函数中实际访问数据对象,或者我甚至无法正确发送数据对象。非常感谢您的帮助

http.post in index.html:

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中的process 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 }));

是nodeJS吗?我在post函数中没有看到任何数据。是的,是nodei。我应该注意,我目前没有在应用程序中执行任何操作。post因为我不确定我现在如何访问传递的数据。您的post函数语法错误。你错过了它的数据。啊,太棒了,是的,只是在Http的angular文档中偶然发现了相同的数据。你可能希望将uibModalInstance.close()放在'then'函数中,以便在出现错误时不会关闭模态。此外,添加catch块以捕获任何错误。通常,我会在“then”函数中添加$scope.success=true,在“catch”函数中添加$scope.suces=false。这将允许我相应地显示一条成功/错误消息。hmmm当我打印req.body.data时,它将留给我未定义。当我删除urlencodedparser时,它将抛出一个错误,表示它无法访问。未定义的body将保留urlencodedparser。通过执行类似{data:'testString'}的操作来测试{data:$scope.data}。并通过console.log(req.body.data)从节点中检索它,但仍然未定义为“testString”