簡體   English   中英

如何在angular js中使用$ http GET方法發送數據?

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

我需要將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');
        });
}

在上面的代碼中,我已將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);
        } 
         });

    };
}

這里我放了console.log(req); 但在“正文”部分中,我只得到像這樣的body{}

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

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

在網址下方用逗號發送這樣的數據

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM