0

I m actually developping an application with angularjs, and I m facing a problem with $http, and the result of an asynchronous service request to my webservice.

Actually, I m using this code :

var promise = undefined;

    UserService.getAll = function (callback) {

        promise = $http({
            url: __ADRS_SRV__ + "users",
            method: "GET",
            isArray: true
        }).success(function(data){
            return data;
        }).error(function(data){
            return $q.reject(data);
        });

        return promise;
    }

This doesnt work, and give me some stuff like, I dont know why :

Angular error returned by the call To know : in my controller, I want to use a really simple syntax like

var data = UserService.getAll();

Do you have any idea how should I process to access my data correctly ?

Thanks for advance

2
  • This looks like a job for $resource rather than $http. Additionally, you need to have a look at callbacks in Javascript because you're not returning what you think you are returning from inside the success/error functions Commented Aug 20, 2014 at 11:17
  • why you return promise ! Commented Aug 20, 2014 at 11:17

2 Answers 2

2

you get the promise in return. There are multiple ways to use this promise.

Example 1 (Use promise in service and return reference of an object):

UserService.getAll = function () {  
    var dataWrapper = {
        myData: {},
        error: null
    };

    $http({
        url: __ADRS_SRV__ + "users",
        method: "GET",
        isArray: true
    }).success(function(data){
        dataWrapper.myData = data;
    }).error(function(data){
        dataWrapper.error = true;
        return $q.reject(data);
    });

    return dataWrapper;
}

Example 2 (Return promise and use it directly in the controller):

// service
UserService.getAll = function () {
    return $http({
        url: __ADRS_SRV__ + "users",
        method: "GET",
        isArray: true
    });
}
// controller
var promise = UserService.getAll();
promise.success(function(data) {
    $scope.data = data;
});

Example 3 (Use regular callback):

// service
UserService.getAll = function (cb) {
    $http({
        url: __ADRS_SRV__ + "users",
        method: "GET",
        isArray: true
    }).success(cb);
}
// controller
UserService.getAll(function(data) {
    $scope.data = data;
});
Sign up to request clarification or add additional context in comments.

4 Comments

I ve only tried the last one actually, and it doesn't work because of $scope not accessible in the service
There are comments to state that this is a part of code in the controller.
I know that, but the callback will be executed with the service context. And in the service, we dont have $scope defined, so it causes an error "undefined is not a function" :/
The callback will be called in the promise context, yes. But you get $scope through the closure. Just be sure that you inject $scope into your controller.
1

The "stuff" you mention is the very promise you create and return.

4 Comments

So how can I access the data ? :-/
the way you do it. In the callback function.
And in my controller ?
the callback function might be defined in the controller. But you can't have asynchrony without callbacks, and $http is asynchronous.

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.