title: $http.get(url) not returning data
body: <p>I am building a service in angular and injecting the service in controller. I am trying to fetch data from json file and am using $http. however the data is not getting returned and i get undefined.</p>
  
  <p>I am updating my code as per suggestion by @Phil</p>
  
  <p><strong>Service.js</strong></p>
  
  <pre><code>;(function(app) {
      app.factory('authService', ['$log', '$http','$location', function($log, $http,$location) {
  
          var url = 'js/user.json';
          var authService= {};
          var userExist=null;
  
          authService.authenticate = function(userid) {
              var userobj = $http.get(url).success(function (data) {
                  userExist = data
                  console.log(data);
                  return userExist;
                  $log.info("Loaded users");
              })
              .error(function (error) {
                  $log.info(error);
                  $log.info("No user exists");
                  return error;
              })
               return userobj;
          }
          return authService;
  
  
      }]);
  })(angular.module('userApp'));
  </code></pre>
  
  <p><strong>Controller.js</strong></p>
  
  <pre><code>;(function(app) {
  app.controller('Controller', ['$scope', '$log','$location','authService', function($scope,$log,$location,authService) {
  
      $scope.data={};
      $scope.getUsers = function()
      {
        userid = "123";
        $scope.data = authService.authenticate(userid);
        console.log($scope.data);
        return $scope.data ;
    }
  
  }]) 
  }(angular.module('userApp')));
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code> &lt;div class="main" ng-controller="Controller"&gt;
  
          &lt;input type="button" name="btngetusers" ng-click="getUsers()"&gt;&lt;/input&gt;
  
      &lt;/div&gt;
  
      &lt;script src ="js/app.js"&gt; &lt;/script&gt;
      &lt;script src ="js/controller/Controller.js"&gt; &lt;/script&gt;
      &lt;script src ="js/services/Service.js"&gt; &lt;/script&gt;
  </code></pre>
  
  <p><strong>user.json</strong>
  i have placed the json file under the js directory.</p>
  
  <pre><code>[
      {
          "UserId": "1",
          "FName": "Hice",
          "LastName": "Harry"
      },
      {
          "UserId": "2",
          "FName": "Andrew",
          "LastName": "Ads"
      }
  ]
  </code></pre>
  
  <p>The data is getting returned as undefined. what am i missing here?</p>
  
  <p><strong>UPDATED CODE</strong></p>
  
  <p>I am updating my code as per suggestion by @skubsi</p>
  
  <p><strong>Service.js</strong></p>
  
  <pre><code>    ;(function(app) {
      app.factory('authService', ['$log', '$http','$location', function($log, $http,$location) {
  
          var url = 'js/user.json';
          var authService = {};
          var userExist=null;
  
          authService.authenticate = function(userid,success,error) {
              $http.get(url).success(function(data){
                  success(data);
              })
              .error(error);
  
          };
          return authService;
  
  
      }]);
  })(angular.module('userApp'));
  </code></pre>
  
  <p><strong>Controller.js</strong></p>
  
  <pre><code>;(function(app) {
  app.controller('MainController', ['$scope', '$log','$location','authService', function($scope,$log,$location,authService) {
      var self = this;
      this.data = null;
  
      this.getUsers = function(){
  
          function success(response){
              self.data = response;
          }
  
          function error(){
              console.log("error");
          }
          authService.authenticate(1,success,error);
      }
  
  
  }]) 
  }(angular.module('userApp')));
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;div class="main" ng-controller="MainController as main"&gt;
               {{main.data}}
              &lt;input type="button" name="btngetusers" value ="Get User" ng-click="main.getUsers()"&gt;&lt;/input&gt;
  
          &lt;/div&gt;
  
      &lt;script src ="js/app.js"&gt; &lt;/script&gt;
      &lt;script src ="js/controller/MainController.js"&gt; &lt;/script&gt;
      &lt;script src ="js/services/authenticate.js"&gt; &lt;/script&gt;
  </code></pre>