Home >Web Front-end >JS Tutorial >About ajax requests in jquery (detailed tutorial)
Below I will share with you a brief discussion of ajax requests in jquery and responses in servlets. It has a good reference value and I hope it will be helpful to everyone.
In jsp, first, you need to import the jquery shelf package:
Get the root path of the returnable site:
<% String path = request.getContextPath(); %>
Write ajax request in jquery:
<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 part:
<p class="tab-pane" id="B" style="text-align:center;"> <p class="row marg" > <table border="2 " style="width:80%;text-align:center;"> <tr class="trtd4"> <th>序号</th> <th>业主名</th> <th>金额</th> </tr> </table> </p> </p>
Alibaba’s fast json conversion package com.alibaba.fastjson.JSON is used in the servlet:
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)); }
The above is me I compiled it for everyone, I hope it will be helpful to everyone in the future.
Related articles:
How to use http module in node.js
How to use js to invoke App in WeChat?
node packaging tool Pkg (detailed tutorial)
The above is the detailed content of About ajax requests in jquery (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!