在jsp中,用ajax发送复杂参数,用new object()组装发送,并接受

用ajax发送请求时,有复杂参数,如下java要接受的参数

	private List piclist;
    private List objanswerList;
    private Long homeworkid;

第一步 js进行组装
ps:piclist和objanswerList是数组

var submitParam = new Object();
 submitParam.piclist = piclist;
 submitParam.objanswerList = objanswerList;
 submitParam.homeworkid = '<%=homeworkid%>';

第二步 发送ajax请求
重点:submitParam: JSON.stringify(submitParam)

$.ajax({
   type:"post",
      url:"url",
      data:{
          submitParam: JSON.stringify(submitParam)
      },
      dataType:"json",
      async:false,
      // headers:{"Content-Type":"application/json"},
      success:function(data){
          if(data.status=="1"){
             
      }
  });

第三步 后台接受
1> 先定义接受参数的bean

public class SubmitParam {
    private List piclist;
    private List objanswerList;
    private Long homeworkid;

    public List getPiclist() {
        return piclist;
    }

    public void setPiclist(List piclist) {
        this.piclist = piclist;
    }

    public List getObjanswerList() {
        return objanswerList;
    }

    public void setObjanswerList(List objanswerList) {
        this.objanswerList = objanswerList;
    }

    public Long getHomeworkid() {
        return homeworkid;
    }

    public void setHomeworkid(Long homeworkid) {
        this.homeworkid = homeworkid;
    }

    @Override
    public String toString() {
        return "SubmitParam{" +
                "piclist=" + piclist +
                ", objanswerList=" + objanswerList +
                ", homeworkid=" + homeworkid +
                '}';
    }
}

2> 使用String submitParamStr = request.getParameter(“submitParam”);接受参数

 String submitParamStr = request.getParameter("submitParam");
 if(submitParamStr!=null&&!"".equals(submitParamStr)){
   SubmitParam submitParam = JsonConvert.parseObject(submitParamStr, SubmitParam.class);
 }

然后就可以使用submitParam 这个参数对象了

给赞呗

你可能感兴趣的:(在jsp中,用ajax发送复杂参数,用new object()组装发送,并接受)