-1
我使用AngularJS構建了一個RESTful API,並且我被卡住了。我正在嘗試發送一個GET請求,該請求將給我一個User
給定的username
。下面是代碼:AngularJS - 狀態碼爲302的HTTP GET請求
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();
}
任何形式的幫助將不勝感激!
哦!這太尷尬了:D非常感謝你,我不知道我在看什麼! –
沒有尷尬的伴侶,這只是新眼睛看着它的效果。多次發生在我身上=) – zameb