ajax传值后台获取初步了解

来源:互联网 发布:gta5卡顿优化 编辑:程序博客网 时间:2024/05/29 11:20
前台页面代码如下:    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    <%          String path = request.getContextPath();          String basePath = request.getScheme() + "://"                  + request.getServerName() + ":" + request.getServerPort()                  + path + "/";      %>            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">      <html>      <head>      <base href="<%=basePath%>">            <title>My JSP 'index.jsp' starting page</title>      <meta http-equiv="pragma" content="no-cache">      <meta http-equiv="cache-control" content="no-cache">      <meta http-equiv="expires" content="0">      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">      <meta http-equiv="description" content="This is my page">      <!--          <link rel="stylesheet" type="text/css" href="styles.css">          -->      <script type="text/javascript" src="index.js"></script>      <script src="<c:url value='/ajax/jquery-2.1.4.js'/>"></script>    <script type="text/javascript">          function add() {                    /**              当给个错误的url的时候,就会走到error方法里面,              该ajax请求会返回一个status code码,当返回值为200时,表示成功,执行success方法,否则执行error方法;               */                  /**              采用json的方式进行数据传递               */              var jsonvalue = {                  "name" : document.getElementById("input1").value,                  "id" : 1,                  "password" : document.getElementById("mima").value,            };               alert("走到了add方法里面");              /**              dataType : "json":的意思是从后台返回的值必须为json格式,否则将执行error方法              */              $.ajax({                  cache : true,                  type : "post",                  url : "<c:url value='/testForcode'/>",                  data : {                      "ss" : JSON.stringify(jsonvalue)                  },                async: false,                  error : function(request) {                      alert("提交失败");                  },                  success : function(data) {                      alert("data:" + data);                      $("#div1").text(data);                  }              });          }      </script>      </head>            <body>          <form id="form1">              用户名:<input type="text" id="input1" name="username" id="yonghu"></input><br />              密码:<input type="password" name="password" id="mima"></input><br />             <input                  type="button" name="提交" value="提交" onclick="add()"></input>              <div id="div1"></div>          </form>      </body>      </html>  
后台代码如下
response.setContentType("text/html");          response.setCharacterEncoding("utf-8");          PrintWriter out = response.getWriter();          /**          * 接受从前端传递过来的json数据,要注意引入解析json的包          */          String name=request.getParameter("ss");        System.out.println(name);        JSONObject json=JSONObject.fromObject(name);          Iterator iter = json.keySet().iterator();               Map<String,String> map = new HashMap<String,String>();               /**             * 解析来自前端页面的值,解析json数组             */             while (iter.hasNext()) {                String key = (String) iter.next();                String value = json.getString(key);                System.out.println("key:"+key+",value:"+value);              map.put(key, value);               }             Map jmap = com.alibaba.fastjson.JSON.parseObject(name);           Map mjmap = new HashMap();           for(Object obj:jmap.keySet()){           mjmap.put(obj, jmap.get(obj));           }           System.out.println(mjmap);                             /**          * 前端页面设置了dataType为json格式,则后台返回时返回的格式必须为json格式          */             out.print("提交成功");          out.flush();          out.close();}

更详细参见:web中用ajax传递json数据到后台



0 0
原创粉丝点击