简体   繁体   中英

$http.post data from modal form (angular)

I'm trying to post the data that is in a modal form on the user hitting send through a HTTP.POST method but I'm having trouble figuring out how to actually access the data object in the post function i have made or if Im even sending it correctly. Any help is greatly appreciated

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');
    };
};

process post function in 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));
})

You missed out the second param in the $http.Post. Your data object is supposed to be passed into the http.Post function as the second param. It can be as such

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

If that is nodeJS + expressJS, you can access the data with req.body.data. It should show the data. If nothing shows with console.log(req.body.data), try removing the middleware urlencodedParser.

Final Update:

Add these two lines to get data from nodeJS + expressJS.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM