当ajax提交多个参数的时候使用serialize()方法

使用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);
		

发布了108 篇原创文章 · 获赞 46 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_44739706/article/details/104635874
今日推荐