后台接受前台传值的几种方法
1 最土的方法(跟标题不搭) 前台,逗号拼接拼成字符串 后台,String[] arr = ids.split(",") 2 默认的contentType: "application/x-www-form-urlencoded", 前台 JSON.stringify(ids) 后台 public RspResult test(String array) { List<String> ids = JSON.parseArray(ids,String.class) } 注明 array:"["1","2"]" 这种方法也可以接受对象数组 3 默认的contentType: "application/x-www-form-urlencoded", 前台 不需要JSON.Stringfy(),正常传一个数组 后台 public void test(HttpServletRequest req) throws IOException { String[] array = req.getParameterValues("ids[]"); ... } 注明 ids[],不能少了[],不然array 为 null 4 contentType : "application/json" 前台 JSON.stringify(arr) 后台 public RspResult get(@RequestBody String[] array) { ... } 这里有个奇怪的现象 前台 content-type默认值 JSON.stringify() 后台 public void test(HttpServletRequest req) throws IOException { String[] array = req.getParameterValues("ids"); return ; } 注意:array[0]:["1","2"] 5 利用框架特性,后台直接用一个list接收 js: var arr = [] arr.push($(this).val()); Controller: @Controller public void test(@RequestParam(required = false, value = "arr[]" List<String> arr)) { } 这样就直接调用了!
