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);
  });
});
2
  • what do you want to do and what is the error? Commented Oct 23, 2016 at 18:30
  • want to get a json format data from a REST API... the error is that the angularJS variable that i pass to the node server is undefined. Commented Oct 23, 2016 at 18:53

1 Answer 1

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

6 Comments

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
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.
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"
do i need to change it in the angular file to?
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.
|

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.