servlet与ajax的结合

来源:互联网 发布:怎么玩网络赚钱 编辑:程序博客网 时间:2024/05/17 09:06

html页面

 <input type="submit" id="chaxun" value="查询" onclick="verify()" />

js代码:

function verify() {

            $.ajax({
url : "echarts",
type : "Get",
dataType : "json",
contentType : "application/json; charset=utf-8",
data : {

                                         time : $("#dt").val() + " 00:00:00",
         time1 : $("#dt1").val() + " 23:59:59",
         chaxun : 'hgl'

                                  },
success : function(data) {

                                   $.each(data.name,function(index,dom){

                                                         alert(this)


                                         }

                         }

}

特别注意data传递值的方式有两种:data:"name=admin&age=18"; 和 data:{name:'admin',age:18}

servlet代码:

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

         

              response.setContentType("text/html;charset=utf-8");
      response.setCharacterEncoding("utf-8");

              String chaxun = request.getParameter("chaxun");
      String time3 = request.getParameter("time");
      String time4 = request.getParameter("time1");

              List list = new ArrayList();

                list.add(chaxun);
        list.add(time3);

               list.add(time4);

               JSONObject jsonData = new JSONObject();//创建JSON

               jsonData.put("list", list);//把集合放进json中

               PrintWriter out = response.getWriter();

               out.println(jsonData);

}


注意:out.println 和out.write两者都不刷新页面,只在原来的页面写数据.最终都是重写了抽象类Writer里面的write方法.

print方法可以将各种类型的数据转换成字符串的形式输出。重载的write方法只能输出字符、字符数组、字符串等与字符相关的数据.

0 0
原创粉丝点击