0

i have a problem sending my angularJS POST parameters to my nodejs server... i've seen many topics related to this, tried everything here and nothing works (there were more):

How to pass parameter in Angularjs $http.post

Angular: how to pass $scope variables into the Node.js server.

How to pass client-side parameters to the server-side in Angular/Node.js/Express

How to pass data from AngularJS frontend to Nodejs backend?

my relevant code that is envolved in this problem, handlebars-template:

<div ng-controller='questions'>
  <form method="POST" class="form-inline" class="my-search-menu">
    <button ng-click="search()" class="btn btn-default glyphicon glyphicon-search" type="submit" style="background-color: #85C1E9;"></button>
    <input style="direction: rtl" type="text" name="search_text" ng-model="search_text" class="form-control" placeholder="Search" aria-describedby="sizing-addon3">
  </form>
</div>

AngularJS:

var myapp= angular.module('myapp', []);

myapp.controller("questions", function($scope,$http) {
$scope.search = function () {
    var formdata = {search_text : $scope.search_text};
    $http.post('/search', {params: formdata})
        .success(function (data, status, headers, config) {
            $scope.questions = data;
        })
        .error(function(data, status, header, config){
            $scope.onerror = "Data: " + status;
    });
    console.log(formdata);
    };
});

NodeJS:

app.post('/search', function (req,res,next){
  var search_text = req.query.params.formdata.search_text;
  console.log(req);
  Question.find({$text: {$search: search_text}}).exec(function (err, questions){
    res.json(questions);
  });
});
Community
  • 1
  • 1
GevAlter
  • 99
  • 10

1 Answers1

1

There are few points you are missing. First in the anguar controller

$http.post('/search', {params: formdata})

will send {params:formdata} as request body in the node server.. So in the server end you will receive the data as request.body. correct way to receive the body in this case will be ..

app.post('/search', function (req,res,next){
      var search_text = req.body.params.search_text;
      //TODO
    });

If you want to send the data as parameter then in controller write the function like this...

$http({
        method: 'POST',
        url: '/search/'+paramData,
      }).then(function successCallback(response) {
        //TODO
      }, function errorCallback(error) {
        //TODO
      });

And in the server side...

app.post('/search/:searchText', function (req,res,next){
      var paramData = req.params.searchText;
      //TODO
    });
Shawon Kanji
  • 710
  • 5
  • 13
  • in the server section you really need to add the searchtext to the route? it looks like its just gonna be the string "searchText" instead of the variable itself – GevAlter Oct 24 '16 at 06:40
  • 1
    Yes, you need to add the section :searchText, look how i have added the route, :searchText. This mean it will be passed as parameter. from the front end you will get the value alias as searchText. – Shawon Kanji Oct 24 '16 at 16:53
  • is anyone know what to do? no matter what i changed, also tried what he suggested except of the last part with posting to the route '/search/:searchText' because i don't understand why or how to do it properly - in the example searchText will be just the string "searchText" – GevAlter Oct 24 '16 at 19:21
  • do i need to change it in the angular file to? – GevAlter Oct 24 '16 at 19:25
  • Yes. the right way to send parameter is the second way. In the angular file that paramData is the actual data you want to send to server. And in the server side :searchText is the variable where it gets stored. – Shawon Kanji Oct 27 '16 at 06:43
  • sorry man, i tried adding "/:searchText" to the route. when i do that it seems like the request from the angular file is going to "/search" and it misses the route on the server side.. meanwhile i succeeded to the same process just with jquery without the route... i didnt wanted to that with jquery but its look like there is kind of bug here because in every solution i looked it works the same and somehow i just get the variable undefined no matter what i do. thank you anyway.. – GevAlter Nov 06 '16 at 04:13