如何调用$http在angularJS顺序?但是我需要根据第一个$http的响应调用它第二个$http
How to call $http in angularJS sequentially? But I need to call it second $http based on response of first $http
我必须进行两次$http调用,但第二次调用必须基于第一次响应(仅当有任何错误时)。
最好的选择是在请求中使用.then()。当使用.then时,您可以传入多个将被调用的函数—如果成功则调用第一个函数,如果出现错误则调用第二个函数。所以,它看起来像这样:
//First http call
$http.get('http://your-url.com')
.then(function (trsponse) {
//Call was successful; do something
}, function (response) {
//Call was unsuccessful; Grab whatever you need from the response and make your second http call:
var data = response.data;
var statusCode = response.status;
var statusText = response.statusText;
$http.get('http://your-second-url.com');
});
传递给函数的响应将具有这些属性:
data – The response body
status – The status code of the response (e.g. 404)
headers – Headers on the response
statusText – The text of the status of the response
你可以这样做:
var promise1 = $http.get('firstUrl.com');
promise.then(
function(payload) {
$scope.movieContent = payload.data;
})
.catch(function (error) {
secondUrl();
console.log("Something went terribly wrong.");
});
:
var secondUrl = function(){
var promise2 = $http.get('secondUrl.com');
promise2.then(
// get the data and also other required functions
);
}
相关文章:
- Meteor HTTP调用出现问题
- 如何将控制器http调用转换为接受参数的服务/工厂模式
- 正在等待HTTP调用从函数返回结果
- 如何在 http 调用成功时禁用锚标记
- 角度 2:使用一个 http 调用的结果来执行另一个调用
- 如何使用在 Javascript 中对 API 进行 HTTP 调用
- 如果statusCode不是200,那么从http调用返回什么类型的错误对象
- node.js:在for循环中进行http调用
- 如何在nodejs中的async.each中进行同步http调用
- 如何在node.js中使用promise进行同步http调用
- 当调用成功时,如何从angular 2中的http调用返回数据
- 循环AngularJS$http调用-TypeError:对象不是函数
- 我正在使用 $scope.$apply 在 http 调用后更新对象数组.谁能告诉我正确的方法
- Meteor HTTP 调用不断加载图像
- 进行多个 http 调用并使用承诺合并结果
- $http调用似乎产生了无限$digest循环
- AngularJS:http 链调用问题和 NodeJS:在多个 http 调用中使用变量的问题
- 使用 $watch 在属性更改时执行$http调用的角度服务
- AngularJS:如何使对象中的对象调用后续的http调用
- 如何在 ag-grid 中从 http 调用加载动态列和行(列和记录都应该是动态的)