在jquery的请求ajax与在servlet中的响应ajax
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在jquery的请求ajax与在servlet中的响应ajax,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2932字,纯文字阅读大概需要5分钟。
内容图文
这次给大家带来在jquery的请求ajax与在servlet中的响应ajax,在jquery的请求ajax与在servlet中的响应ajax的注意事项有哪些,下面就是实战案例,一起来看一下。在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部分:
<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>
在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)); }
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
实现jquery在启动页面立刻加载数据
用jquery操作输入框的文本内容
实现输入框与下拉框联动效果
Jquery下拉框数据动态获取该如何实现
以上就是在jquery的请求ajax与在servlet中的响应ajax的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的在jquery的请求ajax与在servlet中的响应ajax全部内容,希望文章能够帮你解决在jquery的请求ajax与在servlet中的响应ajax所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。