2017-04-22 107 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的身體 –