Ajax前台传数组,Java后台接收方式

第一种: 用$.param(xxx,true) 的方式进行序列化,后台用RequestParam接收

    $.ajax({
        type: "post",
        url: url,
        data: $.param({"courseId":courseId,"oldChooseClassIds":oldChooseClassIdArrs,"chooseClassIds":chooseClassIds},true),
        dataType: "json",
        async: false,
        success: function (rs) {
            if (rs.code == '200') {
                oldChooseClassIdArrs = chooseClassIds;
                isOk = true;
            }else{
                layer.alert(rs.msg);
            }
        }
    });

后台:

@PostMapping("/saveOrUpdateCourseClassInfo")
public ResultEntry saveOrUpdateCourseClassInfo(@RequestParam(value = "courseId",required = false) String courseId,
                                               @RequestParam List oldChooseClassIds,
                                               @RequestParam List chooseClassIds){
    jsSysCourseService.saveOrUpdateCourseClassInfo(oldChooseClassIds,chooseClassIds,courseId);
    return ResultEntry.ok();
}

第二种:前端用传统方式序列化,后端用List<实体> 接收

$.ajax({
type: “post”,
url: url,
data: {“courseId”:courseId,“oldChooseClassIds”:oldChooseClassIdArrs,“chooseClassIds”:chooseClassIds},
dataType: “json”,
traditional : true,//用传统方式序列化数据
async: false,
success: function (rs) {
if (rs.code == ‘200’) {
oldChooseClassIdArrs = chooseClassIds;
isOk = true;
}else{
layer.alert(rs.msg);
}
}
});

后端:ao实体接收

@Setter
@Getter
public class ClassInfoAo {
private List oldChooseClassIds;
private List chooseClassIds;
private String courseId;
}

@PostMapping("/saveOrUpdateCourseClassInfo")
public ResultEntry saveOrUpdateCourseClassInfo(ClassInfoAo ao){
    return ResultEntry.ok();
}

第三种:json接收 在做转化

@PostMapping("/saveOrUpdateCourseClassInfo")
public ResultEntry saveOrUpdateCourseClassInfo(String courseId,String oldChooseClassIds,String chooseClassIds){

    JSONArray jsonArray = JSONObject.parseArray(oldChooseClassIds);
    List list = JSONObject.parseArray(jsonArray.toJSONString(), Long.class);

    return ResultEntry.ok();

$.ajax({
type: “post”,
url: url,
data: {“courseId”:courseId,“oldChooseClassIds”:JSON.stringify(oldChooseClassIdArrs),“chooseClassIds”:JSON.stringify(chooseClassIds)},
dataType: “json”,
async: false,
success: function (rs) {
if (rs.code == ‘200’) {
oldChooseClassIdArrs = chooseClassIds;
isOk = true;
}else{
layer.alert(rs.msg);
}
}
});

你可能感兴趣的:(JAVA相关,ajax,java)