angularjs - Angularjs $http 请求等待上一个请求完成
问题描述
我正在发送一个需要很长时间才能响应的请求,同时我还发送了另一个完全独立于第一个请求但第二个请求仅在第一个请求完成时处理的请求。在第一个请求完成之前,即使路由更改也显示为挂起
请帮我解决这个问题。
解决方案
您应该尝试将第二个请求发送到第一个请求的回调中。
例如:
// First GET Request:
$http.get('/firstUrl').then(function(response) {
// Start second request after the first is finished.
return $http.get('/secondUrl');
}).then(function(response) {
// Change route or something else with the second response
console.log(response);
});
这样第二个请求只有在第一个请求完成后才能启动,您也可以在启动另一个请求之前处理第一个请求获得的响应对象。
推荐阅读
- json - 在 JSON 中接收 c# 字典序列化,并且需要在我的 angular 6 (typscript) 网站中对其进行反序列化
- react-native - MapView Callout onPress 未触发
- r - 如何在 af 选择器功能选择功能中使用上传的 .csv 文件。现在它已被 r shiny 中的导入数据集访问
- python-3.x - 获取 pandas 数据框标头并使用它们创建新的 Access 表
- regex - apache忽略url查询参数顺序重写
- python - pandas:从字典的逆映射创建一列
- php - 无法在数据库中上传照片
- asp.net-mvc-5 - 使用 EF 是否有一个内置过程来引用父级的父级?
- docker - 在 docker 容器中运行的服务调用在另一个 docker 容器中的另一个服务中运行的 Web 服务
- python - python的〜在使用布尔值时发生了什么?