0

I need to send emailId as data from angularjs controller to nodejs. I googled it but I didn't get solution, someone please help me.

controller file:

function ManageProductController($http, $scope, $mdDialog, $document, $location, $localStorage)
{
     var vm = this;
     vm.email = $localStorage.email;

     $http({
            url: 'http://localhost:7200/api/manage-product',
            method: 'GET',
            data: {email:vm.email}
        }).success(function(res) {
            //$scope.productlist = res;
            //console.log(res.result);
            vm.result=res.result;

            //vm.docs=res.docs;
        }, function(error) {
            console.log(error);
            alert('here');
        });
}

In the above code I have sent email as data but in node.js file I am not getting in request.

node file:

 router.get('/manage-product', function(req, res){
    //console.log('I received get request');

    console.log(req);
    var findProducts = function(db, callback) {
       var cursor =db.collection('proInfo').find().toArray(function(err, docs){
          if(err){
             callback(new Error("Some problem"));
           }else{
            callback(null,docs);
        } 
         });

    };
}

Here I have put console.log(req); but in the body section I am getting only body{} like this.

4
  • try removing api from your url http://localhost:7200/manage-product Commented Apr 5, 2016 at 7:29
  • using api its working properly, that's not a problem and my question is how do i send data from angular to node using GET method. Commented Apr 5, 2016 at 7:35
  • Should use POST when submitting data to a server. Commented Apr 5, 2016 at 7:37
  • @HemanthkumarHJ see my answer with both GET and POST examples and let know f it works for you. Commented Apr 5, 2016 at 7:44

2 Answers 2

5

With GET you can make use of params and at server you can get that value in req.query see following sample:

 $http({
            url: 'http://localhost:7200/api/manage-product',
            method: 'GET',
            params: {email:vm.email} //at server it will be req.query.email
        }).success(function(res) {

             //access returned res here

        }, function(error) {
            //handle error here
        });

With POST you can make use of data and at server you can get that value in req.body see following sample:

 $http({
            url: 'http://localhost:7200/api/manage-product',
            method: 'GET',
            data: {email:vm.email} //at server it will be req.body.email
        }).success(function(res) {

             //access returned res here

        }, function(error) {
            //handle error here
        });
Sign up to request clarification or add additional context in comments.

Comments

0

send data like this below the url with comma

url: 'http://localhost:7200/api/manage-product',
method: 'GET',
params: {emailData:yourEmaildata}

Comments

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.