首页 > web前端 > js教程 > 正文

有关ajax在jquery中的请求(详细教程)

亚连
发布: 2018-06-11 10:31:04
原创
1251 人浏览过

下面我就为大家分享一篇浅谈ajax在jquery中的请求和servlet中的响应,具有很好的参考价值,希望对大家有所帮助。

在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][&#39;name&#39;] + "</td><td>"
                    + data[i][&#39;price&#39;] + "元</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));
		}
登录后复制

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在node.js中如何使用http模块

在微信中如何使用js唤起App?

node打包工具Pkg(详细教程)

以上是有关ajax在jquery中的请求(详细教程)的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!