我嘗試了不同的方法來訪問catch塊中的頭文件,但無法實現該功能。
首先嚐試:
function removePaymentAccount(currenAccountDetails) {
var q = $q.defer();
$http({
url: UtilityService.createUrl(serverUrl, 'payment', 'removeMethod'),
method: 'POST',
data: currenAccountDetails
}).then(function(response) {
q.resolve(response);
})
.catch(function(err, status, headers) {
console.log(headers);
q.reject(err);
});
return q.promise;
}
控制檯:
undefined
第二次嘗試:
function removePaymentAccount(currenAccountDetails) {
var q = $q.defer();
$http({
url: UtilityService.createUrl(serverUrl, 'payment', 'removeMethod'),
method: 'POST',
data: currenAccountDetails
}).then(function(response) {
q.resolve(response);
})
.catch(function(err) {
console.log(err.headers('status-msg-header'));
q.reject(err);
});
return q.promise;
}
控制檯::
TypeError: err.headers is not a function
at payment.api.js:72
at processQueue (angular.js:14569)
at angular.js:14585
at Scope.parent.$get.Scope.$eval (angular.js:15848)
at Scope.parent.$get.Scope.$digest (angular.js:15659)
at Scope.parent.$get.Scope.$apply (angular.js:15953)
at angular.js:16248
at completeOutstandingRequest (angular.js:5396)
at angular.js:5668
第三次嘗試:
function removePaymentAccount(currenAccountDetails) {
var q = $q.defer();
$http({
url: UtilityService.createUrl(serverUrl, 'payment', 'removeMethod'),
method: 'POST',
data: currenAccountDetails
}).then(function(response) {
q.resolve(response);
})
.catch(function(err, status, headers) {
console.log(headers('status-msg-header'));
q.reject(err);
});
return q.promise;
}
控制檯::
same as of second
第四次嘗試:
function removePaymentAccount(currenAccountDetails) {
var q = $q.defer();
$http({
url: UtilityService.createUrl(serverUrl, 'payment', 'removeMethod'),
method: 'POST',
data: currenAccountDetails
}).then(function(response) {
q.resolve(response);
}, function(response) {
console.log(response.headers);
q.reject(response);
});
return q.promise;
}
控制檯::
undefined
第五次嘗試:
function removePaymentAccount(currenAccountDetails) {
var q = $q.defer();
$http({
url: UtilityService.createUrl(serverUrl, 'payment', 'removeMethod'),
method: 'POST',
data: currenAccountDetails
}).then(function(response) {
q.resolve(response);
}, function(response, status, headers) {
console.log(headers);
q.reject(response);
});
return q.promise;
}
控制檯:
undefined
我做很多變化,但無法訪問(不張貼整個列表)。任何人都可以請幫我知道我錯過了什麼。
你試過'finally'而不是'catch'嗎? – pherris
不確定.catch承諾回調獲取與您可以提供給'.then'的錯誤回調相同的參數。嘗試使用錯誤回調代替.catch承諾(即'.then(function(){success stuff},function(response){error stuff here)'),檢查[$ http](https:// docs.angularjs.org/api/ng/service/$http)doc的實際使用語法 –
讓我試試看。 –