2

I'm trying make a server call after a response server like this

 $http.post($scope.serviceSave, {
            'person': $scope.person
        }).success(function(data){
            $http.get($scope.serviceList);
        });

but when I tried it I got

Error: $digest already in progress 

and didn't made the request.

I'm tring using jquery like this

<a href="#" ng-click="setContent('/people/list')" >People</a>

$scope.setContent = function(service){
            $http.get(service);
}

then

 $http.post($scope.serviceSave, {
            'person': $scope.person
        }).success(function(){
            $("a:contains(People)").click();
        });

I can make the request, forcing click, but I'm getting

Error: $digest already in progress

yet

please, What's the correct form?

Javier Gutierrez
  • 549
  • 5
  • 16

1 Answers1

0

I tried to reproduce this in JsFiddle and could not do that. It looks like calling one request in callback of another is perfectly valid. Are you using $http service for each request? Not doing so is asking for trouble.

Working example (keep in mind I'm addressing mocked services there, which might be a factor):

$http.post("/echo/json/").success(function() {            
        console.log("finished 1st");
        $http.get("/echo/json/").success(function() {
            console.log("finished 2nd");
        });
});

However calling one callback from within another callback is considered a bad practice. As mentioned before (by someone), using deferred is much more better:

var deferred = $q.defer();
var promise = deferred.promise;

$http.post("/echo/json/").success(function() {            
    console.log("finished 1st");
    deferred.resolve();            
});

promise.then(function(){
    $http.get("/echo/json/").success(function() {
        console.log("finished 2nd");
    });
});

Working fiddle for example above:

ŁukaszBachman
  • 33,595
  • 11
  • 64
  • 74