2017-04-22 78 views
0

我想发布的模式形式的用户打通过HTTP.POST方法发送的数据,但我很难找出如何实际访问我已经发布的post函数中的数据对象,或者如果我甚至发送它正确。任何帮助是极大的赞赏

http.post 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:

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)); 
}) 
+0

那是nodeJS吗? – codemax

+0

我在帖子功能中看不到任何数据。 –

+0

是的,它是节点 –

回答

1

你错过了在$ HTTP第二PARAM。帖子。您的数据对象应该作为第二个参数传递给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 })); 
+0

啊真棒是啊只是偶然发现在http文件的角度相同 –

+0

你可能想要把你的uibModalInstance.close()放在'then'函数中,这样它就不会在出现错误时关闭模式。此外,添加一个catch块来捕获任何错误。通常,我会在'then'函数中添加一个$ scope.success = true,并在'catch'函数中添加一个$ scope.sucess = false。这将允许我相应地显示成功/错误消息。 – codemax

+0

嗯它的离开我与undefined当我打印req.body.data当我删除urlencodedparser它会抛出一个错误,说它不能访问。undefined的身体 –