javascript - AngularJS 中的多个 $http 调用

标签 javascript angularjs rest http

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

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

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

有人知道我能做什么吗?

最佳答案

嗨,因为这是一个 promise ,您可以将下一个调用链接到 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); 
   })

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

关于javascript - AngularJS 中的多个 $http 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24561660/

相关文章:

javascript - 函数未在 javascript 中调用

javascript - 谷歌地图在 PhoneGap 中加载 map 很慢

javascript - 如何从 Angular Controller 中的 Phonegap 插件访问值?

angularjs - 如何在useminPrepare中为HTML文件中的每个 block 定义单独的流程?

api - 是什么让 Flickr API 不是 RESTful?

javascript - 如何为下拉菜单添加滚动条

javascript - 如何使用 Chrome 扩展修改网站元素

javascript - 我能找到文件的大小并保存该文件吗

api - 基于 REST API 的 SDK - 开销?

php - Yii2 REST 简化 BasicAuth