0

伙计们,我有一个问题,即对 j_spring_security_check 的成功 ajax POST (200) 请求没有正确重定向到 success.jsp 页面 - 该页面与登录页面位于同一目录中。login.jsp 页面只是重新显示 - 我不知道为什么(请注意,我对 ajax 还很陌生)。

以下是我的 login.jsp 页面的内容。任何帮助都会得到很大的帮助:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <%--<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>--%>
    <title>Money Tracker - Logon</title>
  </head>

  <body>
      <div>
        <header>
        </header>
        <section>
          <div id="dialog" title="Login">
            <form id="login">
                <label for="userName">User:</label>
                <input id="userName" type="text" name="userName">
                <br>
                <label for="password">Password:</label>
                <input id="password" type="password" name="password">
                <br>
                <label for="rememberMe">Remember me</label>
                <input id="rememberMe" type="checkbox" name="rememberMe"/>
                <br>
               <button type="submit">Login</button>
            </form>
        </div>
    </section>
</div>
<script>

function ajaxLogin(form) {
    var userName = form.userName.value;
    var password = form.password.value;
    var rememberMe = form.rememberMe.value;
    $.ajax({
        cache: false,
        type: 'POST',
        url: "/server/j_spring_security_check",
        crossDomain: true,
        async: false,
        data: { j_username: userName, j_password: password, _spring_security_remember_me: rememberMe },
        beforeSend: function (xhr) {
            xhr.setRequestHeader("x-ajax-call", "true");
        },
        success: function (result, xhr) {
            alert("Request was success. Status is: " + xhr.status);
            if (result == "ok") {
                alert("Result was OK. Status is: " + xhr.status);
                window.location.href = "success.jsp";
            } else {
                alert("Result was not OK. Status is: " + xhr.status);
                location.href = "loginFailed.jsp";
            }

        },
        error: function (xhr) {
            alert("Error with request. Status is: " + xhr.status);
            location.href = "loginFailed.jsp"
        },
        complete: function(result) {
        }
    });
}

$(function () {
    $("#login").submit(function () {
        ajaxLogin(this)
    });
});
</script>
</body>
</html>
4

1 回答 1

0

@Ruaghain 我有一个建议给你。Ajax 的目的不是提交整个表单,它只是向服务器发送少量请求并在同一页面内加载响应。当我检查您的代码时,您的意图是将页面重定向到成功/失败响应。您为什么不提交表单并根据需要重定向到页面。无论如何,您正在使用服务器端验证来验证用户。希望我让你明白了..

于 2013-10-12T18:48:11.300 回答