我正在使用AngularJS构建一个RESTful API,但我遇到了困难。我正在尝试发送GET请求,该请求会在给定User
的情况下向我提取username
。这是代码:
var loginModule = angular.module('login-module', []);
loginModule
.factory('loginService',['$http','$rootScope', function($http){
var factoryMethods = {};
factoryMethods.login = function(username){
return $http.get('http://localhost:8080/WebProjekat/webapi/users/'+username);
}
factoryMethods.setLoggedUser = function(user){
$rootScope.loggedUser = user;
}
return factoryMethods;
}])
.controller('loginController', ['$scope', '$http','loginService', function ($scope, $http, loginService){
$scope.username = '';
$scope.password = '';
$scope.user = {};
$scope.userLogin = function(){
loginService.login($scope.username)
.then(function(response){
console.log("User :" +angular.toJson(response.data)+"successfully logged in!");
$scope.user = response.data;
loginService.setLoggedUser($scope.user);
},function(error){
console.log("Error occured durring user authentication!");
});
}
}]);
由于某种原因,这个函数产生了许诺:
factoryMethods.login = function(username){
return $http.get('http://localhost:8080/WebProjekat/webapi/users/'+username);
}
被拒绝,我得到的状态代码是302.我不明白为什么重定向发生,为什么数据映射到第二个回调函数,当promise被拒绝时调用。
我遵循执行流程,Jersey Servlet成功找到了UserResource
类以及需要调用的服务,以便使用给定的用户名获取用户。这是Java代码:
/**
* URL: http://localhost:8030/WebProjekat/webapi/users/{userId}
* @param username - identification of some user that is registered on the system
* @param uriInfo - variable which can manipulate URL
* @return user with the given username
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{userId}")
public Response getUserById(@PathParam("userId") String username, @Context UriInfo uriInfo) throws JsonParseException, JsonMappingException, IOException {
User user = new User();
List<User> listOfUsers = userService.getAllUsers();
if((user = userService.getUser(username, listOfUsers))!=null) {
user.addLink(getUserSectionsUri(uriInfo, user), "sections");
user.addLink(getSelfUri(uriInfo, user), "self");
return Response.status(Status.FOUND)
.entity(user)
.build();
}
return Response.status(Status.NOT_FOUND)
.build();
}
任何形式的帮助将不胜感激!
答案 0 :(得分:0)
你应该在servlet中返回Status.OK(200)而不是Status.FOUND(302)