伙计们,我有一个问题,即对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>
答案 0 :(得分:0)
@Ruaghain我有一个建议给你。 Ajax的目的不是提交整个表单,它只是向服务器发送少量请求并在同一页面内加载响应。当我检查您的代码时,您的意图是重定向页面上的成功/失败响应。为什么不提交表单并重定向到页面,无论您需要什么。无论如何,您正在使用服务器端验证来验证用户。希望我能让你有所帮助..