Multiple $http calls in AngularJS

Fowarek

So here is what I need to do. I'm using the Angular Deferred Bootstrap library in my code because I need to receive some essential data from a RESTful server before bootstrapping and trying to load the content. Anyway, I must make a second call once the first call resolves. The second call is a login that depends on some URL that is contained in the first response.

Right now, I want to try to make the login call once I receive that data (I was trying in the .success() block) but once the first call resolves, the program begins bootstrapping before the login call is finished; things break because I'm not "logged in" on the server.

window.deferredBootstrapper.bootstrap({
                element: window.document.body,
                module: 'app',
                resolve: {
                    STARTUP_CONFIG: ['$http', function($http) {
                        return $http.get(url1 + 'blah/blah/blah/' + layout);
                    }],

                    CONTEXT_CONFIG: ['$http', function($http) {
                        return $http.get(url1 + 'blah/blah/blah/blah').
                        success(function(data) {
                            $http.post('https://' + data.url2 + '/10001/guestidentity?client_id=' + data.id).
                            success(function(result){
                                token = result.token;
                            });
                        });
                    }],
                }
            });

Anyone have any idea what I can do?

Dayan Moreno Leon

hi since this is a promise you can chain the next call to the then function something like

  $http(url).
  then(function(response){
           //note that this shouldn't be the body of the response
           // but the entire response stream so you need to check for the status code and the body and apply and possibly apply any transformation that is needed as it will probably be just text
             // then we can start the other call
             return $http(url); 
   })

this way the second promise will be handled to the final destination.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related