ajax多数据传送

ajax传送多数据以及servlet解析

前台javascript

 //data可添加多个参数
   	  var url = "findPassword";
   	  var data="{email:'" + document.getElementById("email").value + "',tel:'" + document.getElementById("tel").value + "'}";/*  "{email:'" + document.getElementById("email").value + "',tel:'" + document.getElementById("tel").value + "'}"*/
   	/*   alert(data); */
         /*检测用户是否存在*/
        function callbackFn(result) {
         };
         var dataType ="text";
         $.post(url, data, callbackFn, dataType);//ajax       
   }

后台servlet解析所需的jar包

  1. 导入所需net.sf.json和org.json jar包
    https://pan.baidu.com/mbox/homepage?short=dGUHWOh.
  2. servlet中相关代码
 arg0.setCharacterEncoding("utf-8");
    String i=new BufferedReader(new InputStreamReader(arg0.getInputStream(),"UTF-8")).readLine();

    JSONObject object = JSONObject.fromObject(i);
    
//      System.out.println(object);
//      
//      System.out.println(object.getString("email"));
//      System.out.println(object.getString("tel"));
    String email=object.getString("email");
    String tel=object.getString("tel");

你可能感兴趣的:(java,ee,java)