AngularJS 1.6 升级代码以登录不再使用 REST?
Posted
技术标签:
【中文标题】AngularJS 1.6 升级代码以登录不再使用 REST?【英文标题】:AngularJS 1.6 Upgrade code to login no longer working with REST? 【发布时间】:2018-07-27 11:59:42 【问题描述】:我有一个使用 AngularJS 1.5.8 的项目,登录方法如下:
$scope.login = function()
// creating base64 encoded String from user name and password
var base64Credential = btoa($scope.username + ':' + $scope.password);
// calling GET request for getting the user details
$http.get('user',
headers :
// setting the Authorisation Header
'Authorization' : 'Basic ' + base64Credential
).success(function(res)
$scope.password = null;
if (res.authenticated)
$scope.message = '';
// setting the same header value for all request calling from
// this application
$http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
AuthService.user = res;
$rootScope.$broadcast('LoginSuccessful');
$state.go('dashboard');
else
$scope.message = 'Login Failed!';
).error(function(error)
$scope.message = 'Login Failed!';
);
;
这是通过这个请求使用 spring boot 从数据库中获取信息
@RequestMapping("/user")
public Principal user(Principal principal)
return principal;
代码必须更新才能在 AngularJS 1.6.8 上运行,所以我一直在关注我在网上找到的教程等,现在有了这个:
$scope.login = function()
// creating base64 encoded String from user name and password
var base64Credential = btoa($scope.username + ':' + $scope.password);
// calling GET request for getting the user details
$http(
url: 'user',
method: 'GET',
headers :
// setting the Authorisation Header
'Authorization' : 'Basic ' + base64Credential
)
.then(function onSuccess(res)
$scope.password = null;
if (res.authenticated)
$scope.message = '';
// setting the same header value for all request calling from
// this application
$http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
AuthService.user = res;
$rootScope.$broadcast('LoginSuccessful');
$state.go('dashboard');
else
$scope.message = 'Login Failed!';
, function onError(res)
$scope.message = 'Login Failed!';
);
;
问题是我一直登录失败,但用户在数据库中,并且 .success 已被弃用,所以不知道我做错了什么,非常感谢帮助?
【问题讨论】:
对我来说看起来不错。res.authenticated
登录时说什么??
嗨,林感谢您的回复,我收到如下未捕获的引用错误 VM328:1 未捕获的引用错误:res 未在 :1:13 定义
嗯,你检查过你的网络标签了吗?请求成功了吗?
【参考方案1】:
看看这个:Why are angular $http success/error methods deprecated? Removed from v1.6?
success 和 error 方法在幕后做一些工作来解开 response.data。所以你可能需要做
if (res.data.authenticated) ....
【讨论】:
是的,then/catch
您将得到原始响应。使用success/error
,您只是获取了数据元素。
非常感谢 Kyle 和 Pop-A-Stash 的帮助以上是关于AngularJS 1.6 升级代码以登录不再使用 REST?的主要内容,如果未能解决你的问题,请参考以下文章
是否可以升级 angularjs 属性指令以在 Angular 4 中使用?
AngularJS Spring Social Facebook CORS 问题
从 Django 1.6 升级到 1.9:python manage.py migrate 失败