我开发了管理员应用 AngularJS 和 JAVA 作为服务器端 Restfull API ,我现在想要如果用户录制了错误的登录信息/ password couple,我想处理错误并在客户端页面显示一条消息,这是我的代码:
@RequestScoped
@Path("auth")
public class LoginResource {
@Inject
private UserServiceLocal userServiceLocal;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response authenticate(Credentials credentials) {
User userLoggedIn = userServiceLocal.authenticate(
credentials.getUsername(), credentials.getPassword());
if (userLoggedIn != null) {
userServiceLocal.setToken(userLoggedIn, TokenUtils.createToken());
return Response.ok(userLoggedIn).build();
} else {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
}
}
这是我的客户端代码:
function login(username, password) {
var deferred = $q.defer();
$http
.post(auth_uri, {
username: username,
password: password
})
.then(
function(response) {
if(response){
userInfo = {
accessToken: response.data.token
};
$window.sessionStorage["token"] = response.data.token;
$rootScope.token = response.data.token;
deferred.resolve(response);
}
},
function(error) {
deferred.reject(error);
});
return deferred.promise;
};
这时我使用登录服务:
function LoginController($scope, $state, authenticationSvc){
$scope.submit = function(credentials) {
authenticationSvc
.login(credentials.username, credentials.password)
.then(
function(response) {
$state.go('dashboard.home');
console.log(response.status);
}, function(error) {
console.log('error.status : ' + error.status)
});
}
};
问题是当我尝试发出错误来测试错误响应时,我在浏览器的控制台上显示错误,但没有陷入错误callbackof
的承诺,有什么问题吗?
答案 0 :(得分:0)
您忘记了$ http回调中的return语句。或者如评论中所建议的那样,你可以返回$ http的结果来简化(它已经是一个承诺)。
public class ForwardBack : MonoBehaviour
{
public float roatationSpeed;
private Transform thisTransform = null;
void Start ()
{
thisTransform = GetComponent<Transform>();
}
void Update ()
{
if(Input.GetKey(KeyCode.RightArrow)
{
thisTransform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.LeftArrow)
{
thisTransform.Rotate(Vector3.left * rotationSpeed * Time.deltaTime);
}
}
}