Angularjs 如何在angular中实现一个链连续/连续的$http POST?

Angularjs 如何在angular中实现一个链连续/连续的$http POST?,angularjs,promise,angular-promise,Angularjs,Promise,Angular Promise,我对AngularJS很陌生,我在学习的过程中不断学习。如何链接连续的$http帖子?我需要来自第一个$http POST的响应数据,以便在第二个$http POST中使用,其中我还需要第二个POST返回的响应 $http({ method: 'POST', url: 'http://yoururl.com/api', data: '{"field_1": "foo", "field_2": "bar"}', headers: {'Content-Type': '

我对AngularJS很陌生,我在学习的过程中不断学习。如何链接连续的$http帖子?我需要来自第一个$http POST的响应数据,以便在第二个$http POST中使用,其中我还需要第二个POST返回的响应

$http({
    method: 'POST',
    url: 'http://yoururl.com/api',
    data: '{"field_1": "foo", "field_2": "bar"}',
    headers: {'Content-Type': 'application/json'}
}).then(function(resp) {

   $scope.data_needed = resp.data_needed;
    // Can't possibly do another $http post here using the data I need, AND get its reponse?
    // Would lead to a nested relationship, instead of adjacent chaining.

}, function(err) {
    // Handle error here.
});
我发现用另一个
.then(function(resp){})将另一个$httppost链接到最后一行代码不是一个选项,原因相同(参考上面代码块中的第一条注释)

有什么建议吗?我所能找到的似乎只是链接$HTTPGETS的示例,它不涉及获取和使用响应。干杯。

这是一条路:

$http({...})
    .then(
        function success1(response) {
            var data = response.data;
            $scope.xxx = data.xxx;
            return $http({...});
        },
        function error1(response) {
            return $q.reject(response);
        }
    )
    .then(
        function success2(response) {
            var data = response.data;
            $scope.yyy = data.yyy;
        },
        function error2(response) {
            // handle error
        }
    );

then()
函数返回承诺(返回$http(…)
部分)时,将使用第二个承诺的解析值调用链接的
then()
。还要注意
return$q.reject(…)
部分,这是流程继续执行第二个错误函数而不是第二个成功函数所必需的。

谢谢,这是有效的!现在我只需要弄清楚如何正确地表达http头
-H“Authorization:JWT”
,在angular中。。。再次感谢!