1

I am still quite new to AngularJS and struggling to figure the following out.

I have quite a few web-services that I need to use, quite a few of them relies on the data from another to successfully make the next call.

For example, the first web-service will retrieve a list of Profiles.

ip.controller("ProfilesCtrl", function($scope, $http) {

  $http.post("Profile_List.asp").success(function(data) {
    $scope.profiles = data;
  }).error(function() {
    alert("An unexpoected error ocurred while loading profiles!");
  });

});

Profiles returns a JSON object. Data returned:

{
  "Success": true,
  "ErrorMessage": "",
  "Objects": [{
    "GUID": "208FF69D-A4EB-4760-B2ED-414C900F4AAC",
    "Name": "John Doe",
    "Status": false
  }, {
    "GUID": "BC5C53FD-5CA7-4DBE-8594-D26AD88B758B",
    "Name": "Jane Doe",
    "Status": true
  }, {
    "GUID": "2FCD677B-DA36-4014-823A-9BDD1A72AD66",
    "Name": "Anonymous",
    "Status": true
  }]
}

Ok, so after I have made the initial call, I need to send the GUID of each Profile Object to another web-service. This service will use the GUID to determine the ID of that specific Profile.

The data from the second web-service will only return the ID for the GUID of the first call.

How can I chain these $http calls? Would it be better to create a new json object and use data from there?

I have done this before using ajax.

*Another question regarding my controller code, is this fine like this or would it be better to maybe do the $http calls as a Service, Provider or Factory? How can I go about doing this?

Any help/links with getting the above to AngularJS code would be appreciated.

Please ask if anything is unclear.

1 Answer 1

2

Simply call execute the next call in your "success" handler.

$http.post("Profile_List.asp").success(function(data) {
    $scope.profiles = data;
    //first call succeeded, and we have the data. call method 2
    executeStep2($scope.profiles);

  })


function executeStep2(profiles)
{
   $http.post("second_method") // etc. (you can just send profiles as post data here)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just a comment: it is best practice to declare the function before it is used (executeStep2 in this case)

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.