伙计们,我有一个问题,即对 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>