Javascript 承诺链接未按预期工作
我正在尝试将承诺链用于从angular应用程序发出的$http.get调用Javascript 承诺链接未按预期工作,javascript,angularjs,angular-promise,angularjs-http,Javascript,Angularjs,Angular Promise,Angularjs Http,我正在尝试将承诺链用于从angular应用程序发出的$http.get调用 $http.get(url, config) .then(newViewRequest) .then(function (response) { // success async $scope.viewRequest1.data = response.data; } 在我的newViewRequest中,我向另一个端点发出了一个新的调用,并且我只需要在newViewRequest
$http.get(url, config)
.then(newViewRequest)
.then(function (response) { // success async
$scope.viewRequest1.data = response.data;
}
在我的newViewRequest中,我向另一个端点发出了一个新的调用,并且我只需要在newViewRequest中的调用成功时将响应发送回。下面是我正在尝试的
var newViewRequest = function (response) {
var url1 = $rootScope.BaseURL;
var config = {
headers: {
'Authorization': `Basic ${$scope.key}`,
'Prefer': 'odata.maxpagesize=2000'
}
};
var newresponse = $http.get(url1, config);
if (newresponse.status = 200)
return newresponse;
else return response;
};
但它总是发送请求-响应,而不验证状态或任何东西。我怎样才能做到这一点 newViewRequest中的
$http.get
返回一个承诺。您需要等待它解析以获取状态。您必须从newViewRequest
返回承诺才能进行正确的链接
return $http.get(url1, config)
.then(newresponse => {
if (newresponse.status = 200)
return newresponse;
else return response;
})
.catch(err => {
return response;
})
如果呼叫失败,我仍然无法获得
响应
fails@trx您需要捕获它因错误而失败的情况。我相应地更新了答案。