利用ajax传list道后台,后台返回list到ajax

后台代码:

HttpServletResponse response = ServletActionContext.getResponse();

			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			PrintWriter out = null;
			JSONArray mlist = JSONArray.fromObject(list); //有依赖jar包
			try {
				out = response.getWriter();
				out.print(mlist.toString());			
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				out.flush();
				out.close();
			}

JSONArray依赖jar包:

  • commons-lang.jar

  • commons-beanutils.jar

  • commons-collections.jar

  • commons-logging.jar 

  • ezmorph.jar

  • json-lib-2.2.2-jdk15.jar

如果还是报错,看看包导的正确不正确。其中,commons-lang.jar 和 commons-lang3-3.2.jar是不一样的


前台代码:

$.ajax({ 
    type:"POST",		
    url:"accountAction!queryUMajor",	
    data:"deptId="+$("#deptId").val(),	
    dataType:"text",

    success:function(list){
	var majorList=eval("("+list+")");//处理,将json字符串转换为对象    
	$.each(majorList, function (i, item) { 
		alert(item.id + "," + item.name); 
	});	
    },
    error:function(err){
	alert(err);
    } 
});


你可能感兴趣的:(ajax)