淺談ajax在jquery中的請(qǐng)求和servlet中的響應(yīng)
在jsp中,首先,你需要導(dǎo)入jquery的架包:
獲取可返回站點(diǎn)的根路徑:
<% String path = request.getContextPath(); %>
在jquery中寫(xiě)ajax請(qǐng)求:
<script type="text/javascript">
$(function(){
$(".B").click(function(){
$.ajax({
type: "GET",
//對(duì)應(yīng)servlet中的方法
url: "<%=path%>" + "/queryEvaluateByuserId.do",
//返回是json數(shù)據(jù)
dataType: "json",
async:false,
data:{
},
success: function(data){
str = "";
if(data != null){
//循環(huán)表單列表
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>序號(hào)</th>
<th>業(yè)主名</th>
<th>金額</th>
</tr>
</table>
</div>
</div>
在servlet中用到了阿里巴巴的快速轉(zhuǎn)換json的包c(diǎn)om.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鍵值對(duì)轉(zhuǎn)換成json,傳給jsp
response.getOutputStream().write(JSON.toJSONBytes(workers));
}
以上這篇淺談ajax在jquery中的請(qǐng)求和servlet中的響應(yīng)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jquery中$(#form :input)與$(#form input)的區(qū)別
本節(jié)為大家介紹下jquery 中$(#form :input)與$(#form input)的區(qū)別,需要的朋友可以參考下2014-08-08
jQuery使用之標(biāo)記元素屬性用法實(shí)例
這篇文章主要介紹了jQuery使用之標(biāo)記元素屬性用法,實(shí)例分析了jQuery如何控制頁(yè)面,包含元素的屬性、css樣式風(fēng)格、DOM模型、表單元素和事件處理等使用技巧,需要的朋友可以參考下2015-01-01
jQuery插件 tabBox實(shí)現(xiàn)代碼
最近對(duì)js以及jq產(chǎn)生了濃厚的興趣,看到j(luò)q有很多很好用的插件,功能各異,包括webUI,jqGrid等等。心里萌發(fā)了制作屬于自己的jq插件的想法。2010-02-02
基于jQuery實(shí)現(xiàn)Tabs選項(xiàng)卡自定義插件
這篇文章主要為大家詳細(xì)介紹了基于jQuery實(shí)現(xiàn)Tabs選項(xiàng)卡自定義插件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
jQuery實(shí)現(xiàn)移動(dòng) 和 漸變特效的點(diǎn)擊事件
這里給大家分享的是一則使用jQuery實(shí)現(xiàn)移動(dòng)和漸變特效的點(diǎn)擊事件效果,非常簡(jiǎn)單實(shí)用,這里推薦給大家。2015-02-02

