jhon dano
jhon dano

Reputation: 658

Why is my angular service not returning any results

My angular.js service is not returning any results and I can't figure out why. I am not getting any errors and when check the console I cannot see that the server script is being called?

This is my controller function:

$scope.getLeads = function() {
            var promise = 
            LeadsSrv.all();
            promise.then(
            function(d) { 
                $scope.leads = d.data;
                console.log(d);
            },
            function(e) {
                $log.error('failure loading leads', e);
            });
        };
        console.log($scope.leads);

The service being called is this:

LeadApp.factory('LeadsSrv', function($http) {
    return {
        all: function () {
            $http({
                url: '../Data.php',
                method: "POST",
                params: { req_id: 'leads_list', user_id: 1 }
            }).then(function (response) {
                return response;
            });
        }
    }
});

Upvotes: 1

Views: 44

Answers (2)

Coder John
Coder John

Reputation: 786

LeadApp.factory('LeadsSrv', function($http) {
    return {
        all: function () {
            return $http({
                url: '../Data.php',
                method: "POST",
                params: { req_id: 'leads_list', user_id: 1 }
            });
        }
    }
});

Try making the above changes hopefully that will work

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

Your all() method is not returning anything. Returning from inside then() does not return to the outer function. You need to return the $http promise itself

Try

LeadApp.factory('LeadsSrv', function($http) {
    return {
        all: function () {
          // return `$http` promise
          return  $http({
                url: '../Data.php',
                method: "POST",
                params: { req_id: 'leads_list', user_id: 1 }
            }).then(function (response) {
                return response;
            });
        }
    }
});

Upvotes: 1

Related Questions