javascript - passing data using AngularJS $http.get method -


i'm building application using mean stack make use of data retrieved external api. measure hide api key, want make api request server, having problems passing search term angular front-end server.

the code below part of angular controller, should pass request server search term:

myapp.controller('maincontroller', ['$scope','$http', '$location', function($scope, $http, $location){         $scope.submit = function(){         $location.path('/results');         $http({method: 'get', url: '/makesearch', data: {term: $scope.term} });     } }]); 

and following server code parse request using body-parser middleware:

app.get('/makesearch', function(req, res) { console.log("i received command!"); console.log(req.body); }); 

however once try pass/submit search term front-end, empty object on server console. tips on i'm doing wrong? appreciated.

i figured out! @rikky made point body of http request (req.body) empty. logging req console, worked out how search term can sent using method

using params instead of data in request within angularjs controller show in code below:

revapp.controller('maincontroller', ['$scope','$http', '$location', function($scope, $http, $location){  $scope.submit = function(){     console.log($scope.term);     $location.path('/results');     $http({method: 'get',         url: '/makesearch',         params: {term: $scope.term}     }); } }]); 

and on server, logging req.query instead of req.body shown in code below:

app.get('/makesearch', function(req, res) { console.log("i received command!"); console.log(req.query); }); 

thanks guys!


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -