关于springmvc前台传输报错

突然想到之前遇到的问题,小小的写一下,应该还有别的解决办法,有别的办法欢迎指正,学习,谢谢。。

springmvc中前台通过Post方式传输数据json数据时后台报415错,数据类型不支持,此时,需要在ajax请求中加上contentType: "application/json",定义接收数据为json,
$.ajax({
				url:"dept/batchInsert",
				method:"post",
				contentType: "application/json",
				data:JSON.stringify(deptArr),
				dataType:"json",
				success:function(result){
					if(result.code){
						alert("批量新增成功!");
						window.location.href = "dept/dept_list";
					}else{
						alert("批量新增失败!");
					}
				}
				
			});
并且前台接收的时候需要使用@Requestbody让spring自动将其转换成集合形式
@RequestMapping("/batchInsert")
	@ResponseBody
	public Map<String,Object> batchInsert(@RequestBody List<Dept> depts) {
		
		int row = deptService.batchInsert(depts);
		//System.out.println(depts);
		
		Map<String, Object> map = new HashMap<>();
		map.put("code", row>0);
		return map;
	}


猜你喜欢

转载自blog.csdn.net/weixin_41868360/article/details/80751478