当前位置:   article > 正文

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

java后端接收ajax

第一种: 用$.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);
            }
        }
    });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

后台:

@PostMapping("/saveOrUpdateCourseClassInfo")
public ResultEntry saveOrUpdateCourseClassInfo(@RequestParam(value = "courseId",required = false) String courseId,
                                               @RequestParam List<Long> oldChooseClassIds,
                                               @RequestParam List<Long> chooseClassIds){
    jsSysCourseService.saveOrUpdateCourseClassInfo(oldChooseClassIds,chooseClassIds,courseId);
    return ResultEntry.ok();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

第二种:前端用传统方式序列化,后端用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();
}
  • 1
  • 2
  • 3
  • 4

第三种:json接收 在做转化

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

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

    return ResultEntry.ok();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

$.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);
}
}
});

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/280053
推荐阅读
相关标签
  

闽ICP备14008679号