如何在angular js中使用$ http GET方法发送数据?

Hem*_*H J 0 node.js express angularjs

我需要将emailId作为数据从angularjs控制器发送到nodejs。我用谷歌搜索,但没有得到解决方案,请有人帮我。

控制器文件:

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');
        });
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我已经email作为数据发送了,但是在node.js文件中,我没有收到请求。

节点文件:

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

    };
}
Run Code Online (Sandbox Code Playgroud)

console.log(req);在这里输入了内容,但是在正文部分中,我只是body{}这样输入。

Zee*_*mon 5

通过GET使用params和在服务器上,您可以在req.query以下示例中获得该值:

 $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
        });
Run Code Online (Sandbox Code Playgroud)

通过POST使用data和在服务器上,您可以在req.body以下示例中获得该值:

 $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
        });
Run Code Online (Sandbox Code Playgroud)