案例:
后端以Form表单的Content-type形式接收Integer数组,接口和前端代码如下,前后端联调时,后端接收不到前端传递的参数

接口:	@ResponseBody
   		@RequestMapping("/testIntegerArray")
		public Object o(Integer[] ids){ return "" }
		
前端:	var ids = new Array(1,2,3);
		$.ajax({
		    type:"post",
		    url:"testIntegerArray",
		    // data:{"ids":ids}, 请求状态为500
		    // data:{"ids[]":ids}, 请求状态为500
		    success:function(data){
		    }
		});

解决方案一:
前端将数组转成以逗号分隔的字符串,将字符串作为参数传递,Spring可以识别前端传递的参数,并且赋值到数组中

接口不变:
		@ResponseBody
   		@RequestMapping("/testIntegerArray")
		public Object o(Integer[] ids){ return "" }
		
前端:	var ids = new Array(1,2,3);
		var idStr = ids.join();
		$.ajax({
		    type:"post",
		    url:"testIntegerArray",
		    data:{"ids":idStr}, 
		    success:function(data){
		    }
		});

前后端联调之Form表单提交数组

解决方案二:

后端开发人员在接口的参数前使用@RequestParam注解,指定后端参数匹配前端参数的名称

接口使用 @RequestParam 注解:
		@ResponseBody
   		@RequestMapping("/testIntegerArray")
		public Object o(@RequestParam("ids") Integer[] ids){ return "" }

前端不变:	var ids = new Array(1,2,3);
		$.ajax({
		    type:"post",
		    url:"testIntegerArray",
		    data:{"ids":ids}, 
		    success:function(data){
		    }
		});

前后端联调之Form表单提交数组

相关文章:

  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2021-10-10
  • 2021-12-12
  • 2021-04-12
猜你喜欢
  • 2021-09-30
  • 2021-12-12
  • 2021-12-12
  • 2021-12-14
  • 2021-09-18
  • 2022-12-23
  • 2022-02-10
相关资源
相似解决方案