ajax servlet设置响应,在jquery的请求ajax与在servlet中的响应ajax

这次给大家带来在jquery的请求ajax与在servlet中的响应ajax,在jquery的请求ajax与在servlet中的响应ajax的注意事项有哪些,下面就是实战案例,一起来看一下。

在jsp中,首先,你需要导入jquery的架包:

获取可返回站点的根路径:

String path = request.getContextPath();

%>

在jquery中写ajax请求:

$(function(){

$(".B").click(function(){

$.ajax({

type: "GET",

//对应servlet中的方法

url: "" + "/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 +="

" + num + ""

+ data[i]['name'] + "

"

+ data[i]['price'] + "元

"

+ "

";

}

$(".trtd4").after(str);

}else{

}

},

error: function(data){

}

})

});

}

jsp部分:

序号业主名金额

在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 orderList = new ArrayList<>();

List queryEvaluateList = new ArrayList<>();

orderList = orderServiceImpl.queryOrderList(ownerId, null, null, null, null, null);

List> workers = new ArrayList>();

for(int i = 0;i < orderList.size();i++){

Map order = new HashMap();

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 = 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));

}

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值