dimanche 23 octobre 2016

failed bind angularJS variable to nodejs

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




Aucun commentaire:

Enregistrer un commentaire