Mój kod:
var app = angular.module('app',[]);
app.factory('httpRequestInterceptor', function () {
return {
request: function (config) {
config.headers['Authorization'] = 'Basic kiedyswtymmiejscubedzietoken';
return config;
},
'response': function(response) {
var myHeaderData = response.headers('myheader');
window.localStorage.setItem('myheader', myHeaderData);
return response;
}
};
});
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
});
app.controller('RegistrationController', ['$scope','RegistrationService', function ($scope,UserCRUDService) {
$scope.addUser = function () {
UserCRUDService.addUser($scope.user.name, $scope.user.surname, $scope.user.email, $scope.user.password, $scope.user.birthday, $scope.user.position)
.then(function success(response){
$scope.message = 'User added!';
$scope.errorMessage = '';
// window.localStorage.setItem('myheader',response.myheader);
window.location.href = "/index";
},
function error(response){
$scope.errorMessage = 'Error adding user!';
$scope.message = '';
});
}
}]);
app.service('RegistrationService',['$http', function ($http) {
this.addUser = function addUser(name, surname, email, password, birthday, position){
return $http({
method: 'POST',
url: 'http://localhost:8090/registration',
data: {name:name, surname:surname, email:email, password:password, birthday:birthday, position:position},
headers: {'Content-Type': 'application/json'}
}).then(function (response) {
// window.localStorage.setItem('myheader',response.myheader);
})
}
}]);
Problem polega na tym, że nie wysyłam dane postem i w odpowiedzi dostaję header z tokenem, który muszę odebrać i zapisać w localStorage. A jedyne co ten kod zapisuje to myheader: null
. Próbowałem na różne sposoby, w różnych miejscach w kodzie (zakomentowane fragmenty) i jedyne co się zmieniało to zapis: myheader: null
na myheader: 'undefined'
. Proszę o pomoc co jest nie tak.