繁体   English   中英

AngularJS中的多个$ http调用

[英]Multiple $http calls in AngularJS

所以这是我需要做的。 我在代码中使用了Angular Deferred Bootstrap库,因为在引导和尝试加载内容之前,我需要从RESTful服务器接收一些基本数据。 无论如何,一旦第一个电话解决,我就必须打第二个电话。 第二个调用是一个登录,它取决于第一个响应中包含的某些URL。

现在,我想在接收到数据后尝试进行登录调用(我在.success()块中尝试过),但是一旦第一个调用解决了,程序便会在登录调用完成之前开始引导。 事情中断了,因为我没有在服务器上“登录”。

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;
                            });
                        });
                    }],
                }
            });

有人知道我能做什么吗?

嗨,因为这是一个承诺,您可以将下一个调用链接到then函数,例如

  $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); 
   })

这样,第二个承诺将被处理到最终目的地。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM