1.获取input数据封装成json格式提交。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Add User</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resource/script/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button_submit").click(function(){
var name = $("#userName").val();
var pass = $("#password").val();
var user = {userName:name,password:pass};//拼装成json格式
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/user/addUser4",
data:user,
success:function(data){
alert("成功");
},
error:function(e) {
alert("出错:"+e);
}
});
});
});
</script>
</head>
<body>
<form>
<table>
<tr>
<td>账号</td>
<td>
<input type="text" id="userName" name="userName">
</td>
</tr>
<tr>
<td>密码</td>
<td>
<input type="password" id="password" name="password">
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="button" id="button_submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
2.序列化表单提交。
使用jQuery的serializeArray() 方法序列化表单元素
如果表单元素很多,手工拼装成json数据非常麻烦,可以使用jQuery提供的serializeArray()方法序列化表单元素,返回json数据结构数据。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Add User</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resource/script/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button_submit").click(function(){
//序列化表单元素,返回json数据
var params = $("#userForm").serializeArray();
//也可以把表单之外的元素按照name value的格式存进来
//params.push({name:"hello",value:"man"});
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/user/addUser5",
data:params,
success:function(data){
alert("成功");
},
error:function(e) {
alert("出错:"+e);
}
});
});
});
</script>
</head>
<body>
<form id="userForm">
<table>
<tr>
<td>账号</td>
<td>
<input type="text" id="userName" name="userName">
</td>
</tr>
<tr>
<td>密码</td>
<td>
<input type="password" id="password" name="password">
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="button" id="button_submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
参考:https://www.cnblogs.com/luxh/archive/2013/03/14/2960152.html
https://blog.csdn.net/kxj19980524/article/details/84388467