1. 程式人生 > >json 後臺傳list物件到前臺js獲取便利顯示到下拉框,避免亂碼(ssm)

json 後臺傳list物件到前臺js獲取便利顯示到下拉框,避免亂碼(ssm)

1.後臺

@RequestMapping(value="/getGrade",produces="text/json;charset=utf-8")
	@ResponseBody
	public String getGrade(@RequestParam String stu_major,HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		List<Faculty> gradelist=facultyservice.getMajorByName(stu_major);
		JSONArray jsonarray=JSONArray.fromObject(gradelist);
		String jsonString=jsonarray.toString();
		return jsonString;
	}

2.前臺
function getMajor()
	{
		var stu_faculty=$("#stu_faculty").val();
		$.post("getMajor",{stu_faculty:stu_faculty},function(data){
				var html = "";
				$("#stu_major").empty();
				console.log(data);
				$.each(data,function(i,row){
					alert(i+"  "+row.faculty_major);
					html+="<option value='"
						+row.faculty_major
						+"'>"
						+row.faculty_major
						+"</option>";
				});
				console.log(html);
				$("#stu_major").append(html.trim());
		});
	}
	

3.jsp程式碼
				<br /> <br />專   業:<select name="stu_major" id="stu_major" onchange="getGrade()"></select>