论坛首页 Web前端技术论坛

jquery ajax请求Spring mvc收到请求并返回@ResponseBody List<xx> ,ajax不调用 sucess 方法

浏览 9065 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-10-05   最后修改:2012-10-05
var pageIndex = 0;
        var pageSize = 5;
        $(function () {
           pageIndex = 1;
           AjaxGetData(pageIndex, pageSize);
        });

        function AjaxGetData( index, size) {
            $.ajax({
                url: "positionlistajax.htm",
                type: "Get",
                data: "pagenum=" + index + "&pagesize=" + size,
                //dataType: "json",
                success: function (json) {
                    
                     var html = "";
                     html += "<table>";
                     html += "<thead>";
                     html += "<tr><th colspan=7 >Position List</th></tr>";
                     html += "<tr><th>ID</th><th>Name</th><th>Location</th><th>Nature</th><th>Number</th><th>End Date</th><th>Operation</th></tr>";
                     html += "</thead>";
                     html += "<tbody>";      
                   for(position in json){
                   html += "<tr>";
                   html += "<td>"+json[position].id+"</td>";
                       html += "<td>"+json[position].name+"</td>";
                       html += "<td>"+json[position].location+"</td>";
                       html += "<td>"+json[position].nature+"</td>";
                       html += "<td>"+json[position].number+"</td>";
                       html += "<td>"+json[position].endDate+"</td>"; 
                       html += "<td><a href='editposition?id="+json[position].id+"'>Edit&nbsp;<a href='position?id="+json[position].id+"'>View</td>"; 
                       html += "</tr>";
                   }
                  html += "</tbody>"; 
                  html += "<tfoot>";
                  html += "<tr>";
                  html += "<td colspan='7'>";
                  html += "<span>Total Records:" + ${recordCount} + "; Total Page:<span id='count'>" +${pageCount} + "" + "";
                  html += "<a href='javascript:void' onclick='GoToFirstPage()' id='aFirstPage' >First&nbsp;&nbsp; ";
                  html += "<a href='javascript:void' onclick='GoToPrePage()' id='aPrePage' >Pre&nbsp;&nbsp; ";
                  html += "<a href='javascript:void' onclick='GoToNextPage()' id='aNextPage'>Next&nbsp;&nbsp; ";
                  html += "<a href='javascript:void' onclick='GoToEndPage()' id='aEndPage' >Last&nbsp;&nbsp; ";
                  html += "<input type='text' size='4' /><input type='button' value='Jump' onclick='GoToAppointPage(this)' /> ";
                  html += "</td>";
                  html += "</tr>";
                  html += "</tfoot>";
                  html += "</table>";
                  //alert(html);
                   $('#divResult').html("");
                   $('#divResult').html(html);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest);
                    alert(textStatus);
                    alert(errorThrown);
                }
            });
        }


spring MVC 代码 著能成功接收到ajax 请求 只是前台js无法识别返回的list 不执行sucess方法  是需要转换吗还是 我些的js有问题 求解答
@RequestMapping("/positionlistajax.htm")
	public @ResponseBody
	List<Position> listPositionDo(
			Model model,
			@RequestParam(value = "pagesize", required = false, defaultValue = "5") int pagesize,
			@RequestParam(value = "pagenum", required = false, defaultValue = "1") int pagenum) {
		System.out.println("youlai Ajax ");
		List<Position> ret = new ArrayList<Position>();
		Position pos = new Position();

		pos.setId(3);
		pos.setName("tiantian3");
		pos.setEndDate(new Date());
		pos.setLocation("sunch si  sfsdf ");
		pos.setNature("sdfsdf");
		pos.setNumber(31465);
		ret.add(pos);

		pos = new Position();
		pos.setId(4);
		pos.setName("tiantian4");
		pos.setEndDate(new Date());
		pos.setLocation("sunch si  sfsdf ");
		pos.setNature("sdfsdf");
		pos.setNumber(31465);
		ret.add(pos);

		pos = new Position();
		pos.setId(5);
		pos.setName("tiantian2");
		pos.setEndDate(new Date());
		pos.setLocation("sunch si  sfsdf ");
		pos.setNature("sdfsdf");
		pos.setNumber(31465);
		ret.add(pos);

		System.out.println(ret.size());

		return ret;
	}
   发表时间:2012-10-06  
返回的结果是json格式吗?可以debugger看下response是什么东东。。。
0 请登录后投票
   发表时间:2013-02-06   最后修改:2013-02-06
<a href="javascript:void" onclick="">改成
<a href=“#" onclick="">改成
0 请登录后投票
   发表时间:2013-04-29  
var json = $.parseJSON(json);
如果你后台传回的是一个字符串,你可以不用转换,可如果是一个map,转换吧。否则,不认识。
我昨天晚上才把这个问题解决,当然,前天发现的。
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics