ajax提交json数据并使用Springmvc接收实例

1.页面代码

 $.ajax({
                         type: "POST",
                         contentType: "application/json"  ,//必须加
                         dataType: "json",
                         url: "<%=path%>/dataAnalysis/selsPoverty",
                         data:  JSON.stringify({ 'year': year, 'bar': qhdm })  ,
                         success: function (data) {}  }

2.后台代码

@RequestMapping(value = "/selsPoverty", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String selsPoverty( HttpSession session,@RequestBody Map data) {
		SysAdmin admin = (SysAdmin) session.getAttribute(Constant.LOGIN_SESSION);
		
		JSONObject json=new JSONObject(data);
		User user=new User();
	
		user.setPageNum(0);
		user.setInsert_Date(json.getString("year"));
		//user.setInsert_Date(json.getString("year"));
		List  list = userService.getList(user);
		String str="[";//前台所需的数据DATA
		String lables="[";
		if(list.size()!=0){
			for (int i = 0; i 1)?(str.substring(0, str.length()-1)):str;
			lables=(lables.length()>1)?(lables.substring(0, lables.length()-1)):lables;
			
			str+=  "]";
			lables+= "]";
			
		}else{
			return "2";
			
		}
		
		JSONObject rejson=new JSONObject();
		json.put("lab", lables);
		json.put("strs", str);
		
		return rejson.toJSONString();
	}
	


你可能感兴趣的:(ajax提交json数据并使用Springmvc接收实例)