jQuery集合Ajax传值方式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
 <script type="text/javascript" src="js/jquery.min.js"></script>
 
 <script type="text/javascript">
    $(function(){
    	$("#formsubmit").click(function(){
    		
    		//将表单内容序列化为字符串输出
    		//alert($("#form4user").serialize()) ;
    	});
    $("#formsubmit").click(function(){
    		var radio=document.getElementById("radio").checked;
    		alert(radio);
    		/* $.post("AJAXAction",$("#form4user").serialize(),function(data){
        		alert(data);
        	}); */
    	});
    
    });
    /* $.ajax({
        type:"post",
        url:"AJAXAction",
        data:"username=admin",
        beforeSend:function() {
          alert("准备开始请求...") ;
        },
        success:function(data) {
            alert(data) ;
        },
        error:function() {
            alert("产生了错误!") ;
        }
    }); */
    $(function(){
        $("#formsubmit").click(function(){
      	  $.ajax({
      	        type:"post",
      	        url:"AJAXAction",
      	        data:$("#form4user").serialize(),
      	        beforeSend:function() {
      	          alert("准备开始请求...") ;
      	        },
      	        success:function(data) {
      	            alert(data) ;
      	        },
      	        error:function() {
      	            alert("产生了错误!") ;
      	        }
      	    }); 
        })
      });
 </script>
</head>
<body>
 <form action="" method="post" id="form4user">
        用户名:
        <input type="text" name="username" id="username" />
        密码:
        <input type="password" name="password" id="password" />
        性别
        <input type="radio" name="gender" value="men" id="radio"/>
        <input type="radio" name="gender" value="women" id="radio"/>
        兴趣:
        <input type="checkbox" name="instrs" value="singer"/>
        <input type="checkbox" name="instrs" value="reading"/>
        <input type="checkbox" name="instrs" value="hit"/>
        <input type="button" id="formsubmit" />
    </form>
</body>
</html>

//后台代码:

package om.servletjsp.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 使用Ajax传值时不能使用转发,直接写出就可以了
 * @author Administrator
 *
 */
public class AJAXAction extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		//resp.setContentType("application/json;charset=utf-8");
		//从resp中获取输出流
		PrintWriter out=resp.getWriter();
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String gender = req.getParameter("gender");
		String instrs = req.getParameter("instrs");
		
		System.out.println("ouiu======================"+username+"---"+password+"--gender="+gender+"--instrs="+instrs);
		
		if(username!=null) {
			//使用打印流进行输出,不能使用转发的形式
			String str="请求成功!";
			out.print("1");
		}else {
			out.print("0");
		}
		out.close();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置字符编码样式
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		PrintWriter out=resp.getWriter();
		String username = req.getParameter("username");
		if(username!=null) {
			//使用打印流进行输出,不能使用转发的形式
			String str="请求成功!";
			out.print("1");
		}else {
			out.print("0");
		}
		out.close();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值