【发布时间】:2014-10-04 06:01:51
【问题描述】:
我对 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.
});
出于同样的原因,我发现不能将另一个 $http 帖子与另一个 .then(function(resp) {}); 链接到代码的最后一行,原因相同(参考上面代码块中的第一条评论)。
有什么建议吗?我似乎只能找到链接 $http GET 的示例,这些示例不涉及获取和使用响应。干杯。
【问题讨论】:
标签: angularjs promise angular-promise