AngularJS中的基本身份验证

时间:2015-02-19 02:17:19

标签: angularjs

我有一个基本身份验证的RESTful后端服务器。目前我正在使用AngularJS实现前端页面以与服务器进行交互。任何人都可以给我一些关于在AngularJS中实现基本身份验证的提示吗?我需要使用拦截器吗?

由于

2 个答案:

答案 0 :(得分:0)

通过基本身份验证,您的意思是HTTP身份验证?无论如何 - 如果您需要将来自Angular的经过身份验证的请求发送到您的后端服务器,那么您需要使用HTTP拦截器。

我发现这篇文章非常有用,因为我之前在https://auth0.com/blog/2014/01/07/angularjs-authentication-with-cookies-vs-token/实现基于令牌的auth w / Angular - 但是这里有很多资源。

答案 1 :(得分:0)

您可以使用post方法并处理结果(即,如果登录成功将一些标志成功设置为true,则将成功设置为false)。

backendURL = "http://someurl";
postData = { username: 'USERNAME', password: 'password' };
$scope.serverCall = $http.post(backendUrl, postData);
$scope.serverCall.success(function(result,status,header,config){ //handle your result })
$scope.serverCall.error(function(result,status,header,config){ //handle your Exceptions })

注意:登录失败或成功它只会进入success回调。如果后端有异常,那么它只会进入error回调。

在后端

{ success: true } // If Login Success
{ success: false } // If Login Fail

根据此标记,您可以处理它。

我希望这会有所帮助。