方法一:把list字段封装到一个vo里面(如果只有一个参数那么直接写参数就可以了),然后请求参数设置为@RequestBody。
后端代码:
@RequestMapping(value = "/createAttendance") public Result createAttendance(@RequestBody EmployeeAttendanceVO vo) { Result result = new Result(ResultCode.SUCCESS); try { attendanceSumService.createAttendanceData2(vo.getYear(),vo.getMonth(),vo.getData()); } catch (Exception e) { e.printStackTrace(); result = new Result(ResultCode.EROR); result.setMsg(e.getMessage()); } return result; }
public class EmployeeAttendanceVO implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String year; private String month; private List<String> data; public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public List<String> getData() { return data; } public void setData(List<String> data) { this.data = data; } }
前端代码:
$.ajax({ type: "POST", contentType: "application/json" ,//必须加 dataType: "json", url: "<%=path%>/dataAnalysis/selsPoverty", data: JSON.stringify({ 'year': year, 'bar': qhdm }) , success: function (data) {} })
方法二:使用@RequestParam(value = "ids[]") List<Integer> ids 或者 @RequestParam(value = "ids[]") Integer[] ids
后端代码:
@RequestMapping("/test/setNoProcess") @ResponseBody public String setNoProcess(@RequestParam(value = "ids[]") Integer[] ids){ List<Integer> list= Arrays.asList(ids); try{ decDeliverOrderService.getMapper().statusBatchUpdateToInit(list); }catch(Exception e){ log.error(e); return "error"; } return "success"; }
前端代码:
$(function(){ var contacts = []; contacts[0] = 'xiaoming'; contacts[1] = 'daming'; $.ajax({ type:'GET', url:'demo/sendContacts', data:contacts, success:function(result){ alert(result); } }); })