Bort
Bort

Reputation: 241

Where is data in angularjs http post request?

I'm not sure if I'm just being dumb but here goes. I'm trying to send data to my server through a angularjs button which runs a function with HTTP post request. However I'm not sure where the data is stored in the request. This is my button function:

 <script>
        var app = angular.module('buttonApp', []);
        app.controller('myCtrl', function($scope, $http) {
           $scope.myFunc = function(url) {
               console.log("func called");
               $http({
                  method: 'POST',
                  url: url,
                  data: 'some data'
               }).then(function successCallback(response) {
                    console.log(response);
               }, function errorCallback(response) {
                    console.log("Failed connection");
               });
           }
       });
    </script>

Here is my server code:

app.post('/buttonPressed', function(req, res){
  button = true;
  console.log(req);
  res.send("success");
});

If I console.log the 'req' I get a huge JSON structure where I can't find any reference to 'data' that I'm looking for. Am I on completely the wrong track?

Upvotes: 0

Views: 656

Answers (1)

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15292

Use middilware

 app.use(express.bodyParser());

then

app.post('/buttonPressed', function(req, res){
  button = true;
  req.body; //get the body parameter (POST data)

  res.send("success");
});

It will give the post data sent in body

Upvotes: 1

Related Questions