2017-04-22 4 views
0

HTTP.POST 메서드를 통해 전송되는 사용자에게 모달 폼의 데이터를 게시하려고하지만 실제 처리 방법을 찾지 못했습니다. 나는 포스트 함수에서 데이터 객체에 접근한다. 어떤 도움을 크게 index.html을에 모달 폼 (모서리)의 데이터 http http.post

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에 :

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

해당 노드는 있습니까? – codemax

+0

게시 기능에 데이터가 표시되지 않습니다. –

+0

예 노드가 –

답변

1

당신은 $에 http에서 두 번째 PARAM을 놓쳤다. 게시하다. 데이터 객체는 http.Post 함수로 두 번째 매개 변수로 전달됩니다. 그것은 당신이 req.body.data과 데이터에 액세스 할 수 있습니다,

$http.Post('http://127.0.0.1:8081/process_post', { data: $scope.data }) 

그 nodeJS + expressJS 경우 등

로 할 수있다. 데이터가 표시되어야합니다. console.log (req.body.data)에 아무것도 표시되지 않으면 미들웨어 urlencodedParser를 제거하십시오.

최종 업데이트 :

는 nodeJS + expressJS에서 데이터를 얻기 위해이 두 줄을 추가합니다.

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 
+0

아 awesome 예 그냥 각도 문서 http : // –

+0

에 똑같이 우연히 만났습니다 오류가있을 경우 모달을 닫지 않도록 'then'함수 안에 uibModalInstance.close()를 넣을 수 있습니다. 또한 catch 블록을 추가하여 오류를 잡으십시오. 일반적으로 'then'함수 안에 $ scope.success = true를 추가하고 'catch'함수 안에 $ scope.sucess = false를 추가합니다. 그러면 성공/오류 메시지를 적절하게 표시 할 수 있습니다. – codemax

+0

hmmm 그 undefined와 함께 내가 req.body.data를 인쇄 할 때 urlencodedparser를 제거하면 오류가 발생한다. 정의되지 않은 액세스 .body. –