0

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));
})
5
  • Is that nodeJS? Commented Apr 22, 2017 at 4:07
  • I don't see any data in post function. Commented Apr 22, 2017 at 4:08
  • yeah it is node Commented Apr 22, 2017 at 4:08
  • i should note i currently dont do anything in the app.post as im not really sure how i access the data i pass right now Commented Apr 22, 2017 at 4:09
  • your post function syntax is wrong. you missed data for it. Commented Apr 22, 2017 at 4:12

1 Answer 1

1

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 }));
Sign up to request clarification or add additional context in comments.

8 Comments

ah awesome yeah just stumbled across the same in the angular documentation for http
You may want to put your uibModalInstance.close() inside the 'then' function so that it doesn't close the modal if there was an error. Additionally, add a catch block to catch any errors. Typically, I would add a $scope.success = true inside the 'then' function, and a $scope.sucess = false inside the 'catch' function. That will allow me to display a success/error message accordingly.
hmmm its leaving me with undefined when i print req.body.data when i remove the urlencodedparser it throws an error saying it cant access .body of undefined
Keep the urlencodedparser then. Test your { data: $scope.data} by doing something like { data : 'testString' }. And retrieve it from node by console.log(req.body.data)
still undefined as 'testString'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.