Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

My purpose is to consume a REST web service in AngularJS and I am making some trials right now. Below code is not working and throwing following exception. Can you help me identifty the problem?

Thanks in advance.

function getUsersFromLocal($scope,$http)
{
 $http.get('http://localhost:8080/people').
 success(function(data) {
 $scope.data = data;
 });
return data;
}

Error is: TypeError: Cannot read property 'get' of undefined at getUsersFromLocal.

The service is accessible and I tested it through some REST clients.

share|improve this question
    
Is getUsersFromLocal() a controller? – Martin Shishkov Apr 26 '15 at 9:10
    
You need to supply more context. Where is getUsersFromLocal() defined? The ultimate problem is that Angular's $injector service doesn't know to do dependency injection on the call to getUsersFromLocal() (wherever that is in your code). Follow either @zegoline's answer or @pankajparkar's answer to address your problem. – Brian Clapper Apr 26 '15 at 16:17
up vote 1 down vote accepted

If I understood correctly getUsersFromLocal function is inside controller, basically the function parameters are killing the $scope, $http object existence, You need to remove them from parameter & Also removed the return statement which was outside $http which won't work in anyways.

Code

app.controller('mainCtrl', function() {
    $scope.getUsersFromLocal = function() {
        $http.get('http://localhost:8080/people').
        success(function(data) {
            $scope.data = data;
        });
    };

    $scope.getUsersFromLocal(); //call ajax method
});
share|improve this answer
    
Sorry, I didn't know I could choose only one correct answer. I also marked other comments as okay. – emredmrl May 5 '15 at 19:58
    
@emredmrl no problem bro..Glad to help you..Thanks :) let me know if you want any help :) – Pankaj Parkar May 5 '15 at 19:59

Try like this , this way i found on w3school.

var app = angular.module('myApp4', []);
    app.controller('customersCtrl', function($scope, $http) {
        $http.get("http://www.w3schools.com/angular/customers.php")
                .success(function(response) {
                    $scope.data= response.records;
                });
    });
share|improve this answer

if getUsersFromLocal is not a controller or service, and you are invoking this function manually, you should pass $http and $scope objects to it, like this

module.controller('TestController', function($scope, $http) {

  function getUsersFromLocal($scope,$http) {
    $http.get('http://localhost:8080/people').
      success(function(data) {
        $scope.data = data;
      });
  }

  getUsersFromLocal($scope, $http); // pass this services manually
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.