国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

淺談ajax在jquery中的請求和servlet中的響應

2024-05-06 15:22:29
字體:
來源:轉載
供稿:網友

在jsp中,首先,你需要導入jquery的架包:

獲取可返回站點的根路徑:

<%   String path = request.getContextPath(); %> 

在jquery中寫ajax請求:

<script type="text/javascript">     $(function(){        $(".B").click(function(){        $.ajax({            type: "GET",                     //對應servlet中的方法            url: "<%=path%>" + "/queryEvaluateByuserId.do",                     //返回是json數據            dataType: "json",            async:false,           data:{            },            success: function(data){              str = "";               if(data != null){                               //循環表單列表                 for (var i in data)                  {                     var num = parseInt(i) + 1 ;                                             str +="<tr><td>" + num + "</td><td>"                     + data[i]['name'] + "</td><td>"                    + data[i]['price'] + "元</td>"                     + "</tr>";                  }                 $(".trtd4").after(str);               }else{                                }                           },            error: function(data){            }          })       });     }</script>

jsp部分:

<div class="tab-pane" id="B" style="text-align:center;">           <div class="row marg" >             <table border="2 " style="width:80%;text-align:center;">               <tr class="trtd4">                 <th>序號</th>                 <th>業主名</th>                 <th>金額</th>               </tr>                          </table>           </div>         </div> 

在servlet中用到了阿里巴巴的快速轉換json的包com.alibaba.fastjson.JSON:

private void queryEvaluateByuserId(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException{			HttpSession session=request.getSession();			request.setCharacterEncoding("UTF-8");			response.setContentType("text/html");			response.setCharacterEncoding("UTF-8");			Cookie[] cookies = request.getCookies();			int ownerId = 0;			for (int i = 0; i < cookies.length; i++) {	      Cookie cookie = cookies[i];	      if (cookie.getName().equals("ownerId")) {	      	ownerId = Integer.parseInt(cookie.getValue()); 	      }			}			List<Order> orderList = new ArrayList<>();			List<Evaluate> queryEvaluateList = new ArrayList<>();			orderList = orderServiceImpl.queryOrderList(ownerId, null, null, null, null, null);			List<Map<String, String>> workers = new ArrayList<Map<String, String>>(); 			for(int i = 0;i < orderList.size();i++){				Map<String,String> order = new HashMap<String, String>();				order.put("description", orderList.get(i).getDescription());				order.put("name", orderList.get(i).getOwnerName());				System.out.println(orderList.get(i).getDescription());				order.put("type",orderList.get(i).getTypeName());				queryEvaluateList = orderServiceImpl.queryEvaluateListByUserId(orderList.get(i).getId());				order.put("comment", queryEvaluateList.get(0).getComment());				List<Allocation> allocation = orderServiceImpl.queryAllocationByOrderId(orderList.get(i).getId());				order.put("price", String.valueOf(allocation.get(0).getPrice()));				 System.out.println(order);				workers.add(order);			}            //將map鍵值對轉換成json,傳給jsp            response.getOutputStream().write(JSON.toJSONBytes(workers));		}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 黔西县| 准格尔旗| 南漳县| 特克斯县| 华池县| 罗甸县| 沾化县| 中江县| 柳河县| 吉木萨尔县| 沭阳县| 双柏县| 海门市| 济南市| 通海县| 忻城县| 镇远县| 都兰县| 峨边| 湖北省| 页游| 南木林县| 巧家县| 磴口县| 略阳县| 云和县| 和政县| 康乐县| 顺义区| 海口市| 北碚区| 双桥区| 神农架林区| 星座| 将乐县| 高清| 华池县| 济源市| 胶州市| 海口市| 九寨沟县|