5
.factory('Api', function($http) {
         var API = "http://127.0.0.1:4567/";
         return {
             get: function(method) {
                 return $http.get(API + method).success(function(result) {
                     return result;
                 });
             }
         }
     }

Then

console.log(Api.get("MAppData"));

Returns

Object {then: function, success: function, error: function}

Why does it not return the result (response data)?

2
  • 1
    i believe it is returning a promise, stackoverflow.com/questions/12505760/… Commented Oct 1, 2013 at 17:02
  • It might be possible, that it returns some error and you are not catching it. Commented Oct 1, 2013 at 17:04

1 Answer 1

8

$http returns a promise and you need to chain .then() to get the data like this:

Api.get("MAppData").then(function(response){
    var data = response.data;
});
Sign up to request clarification or add additional context in comments.

2 Comments

What if I just want to return the response data instead and not the whole $http object? Or is this against best practice? Thanks for your help!
@subzero You can switch to use $resource, then you should be able to return the $resouce object which is a wrapper of promise and then angularjs can binds it automatically to the view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.