首页 > 解决方案 > Angularjs $http 请求等待上一个请求完成

问题描述

我正在发送一个需要很长时间才能响应的请求,同时我还发送了另一个完全独立于第一个请求但第二个请求仅在第一个请求完成时处理的请求。在第一个请求完成之前,即使路由更改也显示为挂起

请帮我解决这个问题。

标签: angularjsajax

解决方案


您应该尝试将第二个请求发送到第一个请求的回调中。

例如:

// 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);
});

这样第二个请求只有在第一个请求完成后才能启动,您也可以在启动另一个请求之前处理第一个请求获得的响应对象。


推荐阅读