日期:2014-05-16  浏览次数:20460 次

最近项目遇到的问题一 关于jquery与json

本人是采用SSH架构搭建的项目

问题一:存在的问题是? 获取的json数据 不显示。

UserAction代码

/**
	 * 获取用户所有task列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getUserTasksList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String userId = request.getParameter("userId");
		String weekId = request.getParameter("weekId");
		System.out.println("userId:" + userId + ",weekId:" + weekId);
		List<STaskOfweek> list = taskTotalService
				.queryTaskOfweekListByUserIdAndWeekId(Integer.valueOf(weekId),
						Integer.valueOf(userId));
		System.out.println("list.size:" + list.size());

		// request.setAttribute("userTaskList", userTaskList);
		List<UserTask> userTaskList = new ArrayList<UserTask>();
		for (int i = 0; i < list.size(); i++) {
			STaskOfweek sow = list.get(i);
			UserTask ut =new UserTask();
			ut.setTaskId(sow.getTaskId());
			ut.setTaskName(sow.getTaskName());
			ut.setWeekId(sow.getSWeek().getWeekId());
			ut.setByAssessmanId(sow.getByAssessmanId());
			userTaskList.add(ut);
		}
		Map map = new HashMap();
		map.put("userTaskList", userTaskList);
		JSONArray jo = JSONArray.fromObject(map);
		System.out.println("str:"+jo.toString());
		response.getWriter().write(jo.toString());
		
		return null;
	}

?

JSP页面代码

<script type="text/javascript">
var root = "${pageContext.request.contextPath}";
$(document).ready(function(){
         $("#displayTable").click(function(){
                   var url=root+"/getUserTasksList.do";
                   jQuery.post(url,"weekId=14&userId=30",function(data){
                             var m="<table border='1'>";
                             //var items=data.userTaskList;//在此若取不到值可用var items = data[0];代替
                             //var items=eval(data.userTaskList);
                             var items = eval(data);
                             for(var i=0;i<items.length;i++){
                                     var task=items[i];
                                     m=m+"<tr><td>"+task.taskName+"</td><td>"+task.taskId+"</td></tr>";
                             }
                            m+="</table>";
                           document.getElementById("myTable").innerHTML=m;
                },'json');
       });
});

	

</script>
<body>
<input type="button" value="显示我的表格" id="displayTable"/> 
??????????????????
?????????????????? <div id="myTable">测试json数据显示</div>	</body>	

?感谢群里所有好心的朋友,我的问题最终得到解答。

贴出来,作个参考。

<script type="text/javascript">
var root = "${pageContext.request.contextPath}";

$(document).ready(function () {
            var userId =  $('#userId').val();
            $("[id^=weekId]").each(function () {//循环
                var weekId = $(this).val(); 
                $("#myli" + weekId).click(function () {
                    var url = root + "/getUserTasksList.do";
                    jQuery.post(url, "weekId=" + weekId + "&userId=" + userId, function (data) {
                        var jsonDoc = eval('(' + data + ')');
                        var m = "<table border='1'>";
                        var items = jsonDoc[0].userTaskList;
                        for (var i = 0; i < items.length; i++) {
                            var task = items[i];
                            m = m + "<tr><td>" + task.taskId + "</td><td><a href='javascrip