0
我是新来angularjs,我的工作,它使用基于令牌的认证过程的应用程序,我创建了一个身份验证服务,通过它,我得到令牌但是当我使用此服务在其他控制器我越来越$注射器:unpr错误任何帮助将明显。
我的服务:authService.js
var app = angular.module('loginFormApp', []);
app.controller('loginFormCtrl', function ($scope, AuthService) {
$scope.loginCall = function() {
AuthService.authentication($scope.loginId, $scope.password).then(function (token) {
//AuthService.getToken();
});
};
});
app.factory('AuthService', function ($http) {
var cachedToken;
return {
authentication: function (UserName, Password) {
return $http.post("http://103.19.89.152:8080/ccp-services/authenticate", {
'userName': UserName,
'password': Password
})
.then(function (response) {
window.location.href = "http://192.168.1.148:2000/angular/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;
}
}
});
我的仪表盘控制器: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("http://103.19.89.152:8080/ccp-services/dashboardearnfit/fetch", config).success(function(data) {
$scope.dashboardData = data;
});
});
</script>
它重定向到仪表板页面,但我正在使用web服务的网页不是由于工作代币
错误:
以下行Navigated to http://192.168.1.148:2000/angular/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
你能发布完整的错误代码吗? –
@Itsik,我更新了错误信息 –
尝试在没有
看起来你正在注入一个不可用的模块。在这种情况下,当您尝试注入为依赖项时,它看起来像'AuthService'不可用。
您已经定义了两个独立的应用程序(依赖不会在这种情况下共享)。
1:这是一个带数组语法的模块'setter'。
2:这也是一个带数组语法的模块设置器。
更改2:对于getter,这意味着应用程序将共享模块。
或重构您的模块/应用程序依赖关系。
来源
2016-06-20 11:14:33
我有两个不同的页面的login.html和dashboard.html所以我应该有两个像VAR应用= angular.module(“对myApp”页面使用相同的模块名称,[]); –
我authService.js和login.html的编辑并在这两个文件被称为对myApp犯同样的模块名称,请查看更新的authService.js –
第一:404回复:http://192.168.1.148:2000/angular/%7B %7BdashboardData.singleResult.userProfile.memberPhotoPath%7D%7D。 %7B代表'{'。我的猜测是你有这样的图像:''使用'ng-src'代替:''。第二个403意味着禁止,请检查您的服务器的日志。 – Walfrat
假如你使用的NG-应用程序是这样的:
然后,您必须使用依赖链接模块:
或模块 '对myApp' 的注射器将一无所知loginFormApp
来源
2016-06-20 11:52:05 Walfrat
我链接使用依赖模块,因为它消除了$注射器:unpr错误,但错误403来了,正如我在代码 –
更新,因为它导航到dashboard.html页面,但为gettoken()函数返回未定义的对象 –
您可以注入'AuthService',如下所示:
来源
2016-06-20 12:01:03
@ashish库马尔 - PLZ检查上面的代码。 –