[英]AngularJS Interceptor TypeError: Cannot read property 'headers' of undefined
[英]TypeError: Cannot read property 'data' of undefined in Angular with Interceptor
我有一個攔截器在調用我的 RESTful 服務時捕獲 http 錯誤:
services.config(function($httpProvider) {
$httpProvider.responseInterceptors.push('globalInterceptor');
var elementsList = $();
// this message will appear for a defined amount of time and then vanish again
var showMessage = function(content, cl, time) {
$('<div/>')
.addClass(cl)
.hide()
.fadeIn('fast')
.delay(time)
.fadeOut('fast', function() { $(this).remove(); })
.appendTo(elementsList)
.text(content);
};
});
services.factory('globalInterceptor', function($q){
//When the interceptor runs, it is passed a promise object
return function(promise){
//In an interceptor, we return another promise created by the .then function.
return promise.then(function(successResponse){
//Do your code here if the response was successful
if (typeof(successResponse) !== 'undefined') {
if (successResponse.config.method.toUpperCase() != 'GET') {
showMessage('Success', 'http-success-message', 5000);
return successResponse;
}
}
}, function(errorResponse){
switch (errorResponse.status) {
case 400: // if the status is 400 we return the error
showMessage(errorResponse.data.message, 'http-error-message', 6000);
// if we have found validation error messages we will loop through
// and display them
if(errorResponse.data.errors.length > 0) {
for(var i=0; i<errorResponse.data.errors.length; i++) {
showMessage(errorResponse.data.errors[i],
'http-error-validation-message', 6000);
}
}
break;
case 401: // if the status is 401 we return access denied
showMessage('Wrong email address or password!',
'http-error-message', 6000);
break;
case 403: // if the status is 403 we tell the user that authorization was denied
showMessage('You have insufficient privileges to do what you want to do!',
'http-error-message', 6000);
break;
case 500: // if the status is 500 we return an internal server error message
showMessage('Internal server error: ' + errorResponse.data.message,
'http-error-message', 6000);
break;
default: // for all other errors we display a default error message
showMessage('Error ' + errorResponse.status + ': ' + errorResponse.data.message,
'http-error-message', 6000);
}
return $q.reject(errorResponse);
});
}
});
我的登錄頁面在加載時停止。 問題是在制作 Inceptor 之后出現的,在此之前一切正常。
它給了我這個確實無濟於事的錯誤消息:
TypeError: Cannot read property 'data' of undefined
at http://localhost:8080/vendor/angular-1.2.2/angular.js:7479:22
at deferred.promise.then.wrappedCallback (http://localhost:8080/vendor/angular-1.2.2/angular.js:10655:81)
at deferred.promise.then.wrappedCallback (http://localhost:8080/vendor/angular-1.2.2/angular.js:10655:81)
at http://localhost:8080/vendor/angular-1.2.2/angular.js:10741:26
at Scope.$get.Scope.$eval (http://localhost:8080/vendor/angular-1.2.2/angular.js:11634:28)
at Scope.$get.Scope.$digest (http://localhost:8080/vendor/angular-1.2.2/angular.js:11479:31)
at Scope.$get.Scope.$apply (http://localhost:8080/vendor/angular-1.2.2/angular.js:11740:24)
at done (http://localhost:8080/vendor/angular-1.2.2/angular.js:7744:45)
at completeRequest (http://localhost:8080/vendor/angular-1.2.2/angular.js:7918:7)
at XMLHttpRequest.xhr.onreadystatechange (http://localhost:8080/vendor/angular-1.2.2/angular.js:7874:11) angular.js:9159
我試圖調試/console.log 這個“數據”屬性,但找不到它......當應用程序加載時,它會得到兩個文件正常(200 和數據屬性)和其中兩個錯誤。
任何幫助將不勝感激:-)
我沒有看到您的function(errorResponse)有任何問題。 它似乎正確地沿鏈傳遞錯誤。
但是我對你的函數(successResponse)有問題。 只有當兩個 IF 語句都滿足時,它才會將值向下傳遞,而在其他情況下它傳遞 undefined。
我的猜測是您需要在function(successResponse)的末尾添加 $q.reject( ) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.