首頁 > 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學習者快速成長!