满意答案
carolfilan
2016.04.18
采纳率:46% 等级:9
已帮助:816人
给你个例子。
后台Java代码: @RequestMapping(value = "/friendLinks/friendLinksList.do") public void getfriendLinksList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){ response.setCharacterEncoding("UTF-8"); String pageNoStr = request.getParameter("pageNo"); Integer pageNo = 0; try { pageNo = Integer.valueOf(pageNoStr); } catch (Exception e) { pageNo = 0; } Page page = new Page(); page.setPageNo(pageNo); //按分页条件查询用户 List linksList = null; try { linksList = friendLinksService.getFriendLinksList(page); //将查出来的友情链接和分页信息放到json字符串中 JSONArray jsonList = new JSONArray(); JSONObject bj = JSONObject.fromObject(page); jsonList.add(bj); for (FriendLinks links : linksList) { JSONObject obj = JSONObject.fromObject(links); jsonList.add(obj); } response.getWriter().println(jsonList.toString()); response.getWriter().flush(); response.getWriter().close(); } catch (Exception e) { e.printStackTrace(); } return ; }
JS中代码: function fillData(pageNo) { if (pageNo == "") { pageNo = thisPageNo; } var stateValue = $("#stateValue").val(); var titleValue = $("#titleValue").val(); $.ajax({ url:contextPath + "/friendLinks/friendLinksList.do", type:"POST", data:{ pageNo:pageNo, stateValue:stateValue, titleValue:titleValue }, dataType:"json", beforeSend:function () { $("#pageTd a").remove(); }, success:function (data) { var page = data[0]; if (page.count == 0) { var noDataMsg = "
无相关数据"; $("#dataBody").html(noDataMsg); var pageMsg = "总记录数: 0"+ " 当前页: 0/0 "+ " 首页 "+ " 上一页 "+ " 下一页 "+ " 尾页 "; $("#pageButtonBefore").html(pageMsg); } else { var dataMsg = ""; var caozuo = ""; var useOrNot = ""; for (var i = 1; i < data.length; i++) { caozuo = ""; caozuo = caozuo + " 查看"; caozuo = caozuo + " 修改"; caozuo = caozuo + " 删除"; if(data[i].status == "0"){ status = "是"; }else{ status = "否"; } dataMsg = dataMsg + "" + i + "" + data[i].linkName + "" + data[i].linkSite + "" + status + ""; dataMsg = dataMsg + caozuo; dataMsg = dataMsg + ""; } $("#dataBody").html(dataMsg); var pageMsg = "总记录数: "+page.count+" 当前页: "+page.pageNo+"/"+page.totalPage+" "+ " 首页 "+ " 上一页 "+ " 下一页 "+ " 尾页 "; $("#pageButtonBefore").html(pageMsg); } }, error:function () { alert("获取相关数据失败!"); }}); }其实能看出来,JS怎么接收值跟后台用什么方式传没有多大关系,关键是看传过来的值格式是啥就好了。
传回来的是字符串,那就直接按照字符串处理,传回来的是Json格式的那就直接按照解析Json来处理。
00分享举报