【发布时间】:2016-05-01 18:18:24
【问题描述】:
好的,所以我尝试遵循 Spark 文档,我想在我的单页应用程序中执行简单的重定向。我的代码如下所示:
post("/users/login", (req, res) -> {
ObjectMapper mapper = new ObjectMapper();
User creation = mapper.readValue(req.body(), User.class);
User user = userService.getUser(creation.getLogin());
if (user.getPassword().equals(creation.getPassword())) {
req.session().attribute("userid", creation.getLogin());
System.out.println("OK");
res.status(201);
res.redirect("/index.html");
return "";
}
System.out.println("BAD");
return null;
} , json());
基本上,我有三个静态 html 文件:registration.html、login.html 和 index.html。我阅读了有关 staticFileLocation 的内容,因此我在 main 函数的开头添加了以下代码行:
staticFileLocation("/public");
当我输入正确的登录名和密码时,我在 Chrome 的网络视图中发现我收到了状态为 200 的 GET 请求,OK,发往http://localhost:4567/index.html。但是,浏览器什么也不做,也不会将我重定向到该页面。你能告诉我我做错了什么吗?
编辑: 这是处理客户端登录的javascript代码:
app.controller('LoginUserCtrl', function($scope, $http) {
$scope.loginUser = {};
$scope.submitForm = function() {
$http({
method : 'POST',
url : 'http://localhost:4567/users/login',
data : $scope.loginUser,
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).success(function() {
console.log("User logged successfully");
console.log($scope.loginUser);
}).error(function() {
console.log("Unknown error while logging user");
});
};
});
【问题讨论】:
-
我想补充一点,当我发送帖子时,我得到状态 302,然后 GET 状态为 200 的请求,但仍然浏览器(我尝试过 Chrome 和 IE)什么都不做......
标签: http redirect single-page-application http-redirect spark-java