こんにちは、AngularJS の http インターセプターの応答メソッドで非同期呼び出しを実行しようとしています。非同期呼び出しが発生しますが、無限ループに陥り、非同期呼び出しが何度も呼び出されます...理由はわかりませんが、エラーは非同期呼び出しの構造に関係していると思います
var app = angular.module('AceAngularApi', []);
app.service('Ace',['$http','$q','$injector', '$window', function($http, $q, $injector, $window){
this.user = null;
var setToken = function(token){
$window.localStorage.token = token;
};
var removeToken = function(){
$window.localStorage.token = '';
}
var setCurrentUser = function(currentUser){
user = currentUser;
};
var getCurrentUser = function(){
var self = this;
var url = "http://localhost:8080/api/currentuser";
var response = $http.post(url, {}).then(function(response){
if(response.data.data["obj"]){
self.user = response.data.data["obj"];
}
return response;
});
return response;
};
var currentUser = function(){
return user;
};
return {
setToken: setToken,
setCurrentUser: setCurrentUser,
getCurrentUser: getCurrentUser,
currentUser: this.user,
removeToken: removeToken
}
}]);
app.factory('authInterceptor',['$rootScope', '$q', '$window', '$injector',
function ($rootScope, $q, $window, $injector) {
return {
request: function (config) {
config.headers = config.headers || {};
if ($window.localStorage.token) {
config.headers.Authorization = 'Bearer ' + $window.localStorage.token;
}
return config;
},
response: function(response) {
var deferred = $q.defer();
var Ace = $injector.get('Ace');
Ace.getCurrentUser().then(function(){
deferred.resolve(response);
});
return deferred.promise;
}
};
}]);
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('authInterceptor');
});