使用serialize()可以把参数打包到一起,用一个变量接收,然后在ajax的data里面只需要写这个变量的名字就可以了。
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>
$(function(){
$("[type='button']").click(function(){
var str=$("form").serialize();
/* var str=$("form").serializeArray(); */
/* console.log(str); */
$.ajax({
url:"DemoServlet?opr=sel",
data:str,
success:function(){
},
error:function(){
alert(2);
},
})
})
})
</script>
<body>
<form>
用户名:<input type="text" name="user"/>
密码:<input type="password" name="pwd"/>
<input type="button" value="提交"/>
</form>
</body>
DemoServlet页面:
在这边获取的参数方式和原来的不变
String user=request.getParameter("user");
String pwd=request.getParameter("pwd");
System.out.println(user+"..."+pwd);