我是angularjs的新手,我正在开发一个基于令牌的身份验证应用程序,我创建了一项服务,当用户的登录凭据正确时它会做什么然后创建一个令牌并存储在中cachedToken 我可以通过 getToken()功能访问它。但它在仪表板页面中不起作用,并且给对象未定义,不明白该怎么做。
我的authService.js
var app = angular.module('loginFormApp', [])
.factory('AuthService', function ($http) {
var cachedToken;
return {
authentication: function (UserName, Password) {
return $http.post("URL", {
'userName': UserName,
'password': Password
})
.then(function (response) {
window.location.href = "URL/dashboard.html";
cachedToken = response.data.httpHeaders.h5cAuthToken;
return cachedToken;
// alert(token);
},
// Error Handling
function (response) {
console.log(response.datas);
});
},
getToken: function() {
alert(cachedToken);
return cachedToken;
}
}
})
.controller('loginFormCtrl', function ($scope, AuthService) {
$scope.loginCall = function () {
AuthService.authentication($scope.loginId, $scope.password).then(function (token) {
AuthService.getToken();
});
};
});
我的dashboard.html
<script>
var app = angular.module('myApp', ['loginFormApp']);
app.controller('dashboardFormCtrl', function($scope, $http, AuthService) {
var config = {
headers: {
'h5cAuthToken': AuthService.getToken(),
'Accept': 'application/json;odata=verbose'
}
};
//alert(config.headers.h5cAuthToken);
$http.get("URL", config).success(function(data) {
$scope.dashboardData = data;
});
});
</script>
错误:
Navigated to URL/dashboard.html
dashboard.html:126 GET http://192.168.1.148:2000/angular/%7B%7BdashboardData.singleResult.userProfile.memberPhotoPath%7D%7D 404 (Not Found)
dashboard.html:216 GET http://192.168.1.148:2000/angular/%7B%7Bactivityitem.participantPicPath%7D%7D 404 (Not Found)
dashboard.html:289 GET http://192.168.1.148:2000/angular/%7B%7BchallengeItem.participantPicPath%7D%7D 404 (Not Found)
jquery-migrate-1.1.0.min.js:1'//@ sourceURL' and '//@ sourceMappingURL' are deprecated, please use '//# sourceURL=' and '//# sourceMappingURL=' instead.
angular.js:10765 GET http://103.19.89.152:8080/ccp-services/dashboardearnfit/fetch 403 (Forbidden)(anonymous function) @ angular.js:10765r @ angular.js:10558g @ angular.js:10268(anonymous function) @ angular.js:14792$eval @ angular.js:16052$digest @ angular.js:15870$apply @ angular.js:16160(anonymous function) @ angular.js:1679e @ angular.js:4523c @ angular.js:1677yc @ angular.js:1697Zd @ angular.js:1591(anonymous function) @ angular.js:29013b @ angular.js:3057If @ angular.js:3346d @ angular.js:3334