使用Scala Play在没有表单的情况下登录

使用Scala Play在没有表单的情况下登录,scala,playframework,Scala,Playframework,我正在尝试在没有play表单的情况下编写登录页面,并使用post请求发送登录数据。发送帖子后,我的登录页面没有重定向到主页 我怎样才能完成这项任务 我的代码 路线: GET / controllers.Application.index GET /login controllers.Application.login POST /login

我正在尝试在没有play表单的情况下编写登录页面,并使用post请求发送登录数据。发送帖子后,我的登录页面没有重定向到主页

我怎样才能完成这项任务

我的代码

路线:

GET     /                           controllers.Application.index
GET     /login                      controllers.Application.login
POST    /login                      controllers.Application.auth
登录页面

<body>
        <h1>Play! Login Sample - Login</h1>
        <p>Please provide your credentials.</p>
        <p><input type="email" name="email" placeholder="Email" id="email"></p>
        <p>
            <input type="password" name="password" id="password" placeholder="Password">
        </p>
        <p>
            <button type="submit" id="loginbutton" onclick="auth();return false">Login</button>
        </p>
        <script>
                function auth( ){
                    var user = $("input[name=email]").val();
                    var pass = $("input[name=password]").val();
                    console.log(user+" "+pass)
                    xhr = new XMLHttpRequest();
                    var url = "/login";
                    xhr.open("POST", url, true);
                    xhr.setRequestHeader("Content-type", "text/json");
                    var data = JSON.stringify({"name":user, "pass":pass});
                    xhr.send(data);
                    console.log(xhr.status)
                }
        </script>
    </body>

如果控制器响应重定向,则需要重定向客户端。 在xhr.send之前添加响应处理器:

xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
        window.location = xhr.responseText;
    }
}
更新控制器:

 Redirect(routes.Application.index())


作为xhr.responseText,我得到了我的主页HTMLN,我从来没有用重定向做过这件事,所以我现在看到XMLHttpRequest遵循303重定向,不可能阻止它。我建议您将url返回到主页并检查200代码上的响应,然后重定向。我将用绝对url响应更新应答更新
 Redirect(routes.Application.index())
... = Action { implicit request => {
 ...
 Ok(routes.Application.index().absoluteURL())