java後台傳遞多個對象到ajax中,簡單的放入,傳遞與遍曆

來源:互聯網
上載者:User

標籤:cte   org   character   hash   function   flush   tran   object   前端   

其實這種方式已經很少用了,因為我們已經有很成熟的架構可以用,例如stringmvc+hibernate 它們對這些互動有一些很成熟的封裝。

但是當不用架構時,有些東西還是需要知道的。

1.在後台代碼中傳遞參數,放入流中。

@RequestMapping(value="test")public void jjj(HttpServletResponse response,int s,int d){    try {        response.setCharacterEncoding("utf-8");        PrintWriter pw=response.getWriter();        List<Student> list=studentservice.getAllStudentByTeaId("1", 1);        HashMap map=new HashMap();        HashMap map1=new HashMap();        map.put("list",list);        map.put("count", 123);        map.put("count1", 123);        map.put("list1", list);        JSONArray array=JSONArray.fromObject(map);        pw.write(array.toString());        pw.flush();        pw.close();    } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();    }

 2.在前端的擷取中,需要jquery.js

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title><script type="text/javascript" src="js/jquery.js"></script><script type="text/javascript">function btn(){    $.ajax({        url:"test",        type:"post",        data:{            ‘s‘:1,            ‘d‘:2        },        cache:false,        dataType:"json",        success:function(data){            for(var key in data){                  alert(data[key].count);                  alert(data[key].count1);                     $.each(data[key].list,function(index,item){                         $(‘#span‘).html($(‘#span‘).html()+"   學號  :   "+item.id+"  姓名 :"+item.name);                     });                   alert(data[key].list1);                 }         }    });}</script></head><body><span id="span"></span><input type="button" value="點擊" onclick="btn()"></body></html>

 

java後台傳遞多個對象到ajax中,簡單的放入,傳遞與遍曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.