简体   繁体   English

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

[英]How to send data using $http GET method in angular js?

I need to send emailId as data from angularjs controller to nodejs. 我需要将emailId作为数据从angularjs控制器发送到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. 在上面的代码中,我已将email作为数据发送,但是在node.js文件中,我没有收到请求。

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); 这里我放了console.log(req); but in the body section I am getting only body{} like this. 但在“正文”部分中,我只得到像这样的body{}

With GET you can make use of params and at server you can get that value in req.query see following sample: 使用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
        });

With POST you can make use of data and at server you can get that value in req.body see following sample: 通过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
        });

send data like this below the url with comma 在网址下方用逗号发送这样的数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular $ http get方法无法将正确的数据发送到服务器 - Angular $http get method does not send correct data to server 如何在Angular js中的http请求中发送头数据 - How to send header data in http request in Angular js 如何在不使用http.get的情况下使用“服务”方法在其余的角度js中使用“获取”方法(可能使用restangular js) - How to use 'Get' method in rest angular js using 'service' mehtod without using http.get (probably using restangular js) 无法在Angular JS中使用$ http.get调用PHP方法 - Not able to call a method of PHP using $http.get in Angular JS 如何通过angular $http.get 将json数据发送到webapi - How to send json data to webapi through angular $http.get 使用Angular js在Fusion Chart中使用http get请求未加载数据 - Data is not loading using http get request in Fusion Chart with Angular js 如何使用Firebase和Angular发送HTTP GET请求? - How to send an HTTP GET request using Firebase and Angular? 使用$ http()的Angular JS获得状态为200的响应,但数据为null - Angular JS using $http() get a response, with status 200, but data is null 如何使用angular js $ http post调用控制器[HttpPost]方法? - How to call controller [HttpPost] method using angular js $http post? 如何使用角度js中的$ http服务从另一个文件获取数据? - How get the data from another file using $http service in angular js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM