JSP相当新。 我的index.jsp中有以下代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>AJAX Form</title>
</head>
<body>
<form method="POST" id="form1" onsubmit="return validateLoginForm()" >
UserName: <input type="text" name="username" id="username">
<br />
Password: <input type="text" name="password" id="password"/>
<input type="submit" value="Submit" />
</form>
<p id="demo"></p>
</body>
</html>
<script type="text/javascript">
function validateLoginForm() {
var url = "action_form_process.jsp";
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = { "username":username,
"password":password
};
http.open("POST", url, false);
http.setRequestHeader("X-Requested-With","XMLHttpRequest");
http.setRequestHeader("Content-type", "application/json");
http.send(JSON.stringify(params));
return true;
}
</script>
还有另一个JSP'action_form_process.jsp`。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Form Processing</h1>
<p><b>Welcome User: Coder</b>
</p>
</body>
</html>
但是不会发生重定向。 完全没有错误。所有JSP都位于同一文件夹中。
请帮助并解释我在做什么错?