后台接收的实体中包含List,前台ajax如何传参

  前端代码
 function a() {
var redio = $("input:hidden[id='redio']").val();
var checkbox = $("input:hidden[id='checkbox']").val();
var text = $("input:hidden[id='text']").val();

console.info(redio);
console.info(checkbox);
console.info(text);
var answerRecordList =[];//这是后台的list
//单选框
$.each( redio.split(','),function(index,value){
    if(value.length>0){
        console.info("单选框:"+value+":"+$("input[name="+value+"]:checked").val());
        var answerRecord={"questionId":value,"optionId":$("input[name="+value+"]:checked").val()};
        answerRecordList.push(answerRecord);
    }
});
//多选框
$.each( checkbox.split(','),function(index,value){
    if(value.length>0){
        $('input[name='+value+']:checked').each(function(){//遍历每一个名字为interest的复选框,其中选中的执行函数
          //  chk_value.push($(this).val());//将选中的值添加到数组chk_value中
            console.info("多选框:"+value+":"+$(this).val());
            var answerRecord={"questionId":value,"optionId":$(this).val()};
            answerRecordList.push(answerRecord);
        });
    }

});
             //文本框
               $.each( text.split(','),function(index,value){
                   if(value.length>0){
                       console.info("文本框:"+value+":"+$(" #"+value).val());
                       var answerRecord={"questionId":value,"answer":$(" #"+value).val()};
                       answerRecordList.push(answerRecord);
                   }
               });
               var data={
                   "naireId":1,
                   "startTime":"2020-01-01",
                   "telephone":$(" #tel ").val(),"name":$(" #name ").val(),
                   "answerRecordList":answerRecordList//后台list
               }
//alert(JSON.stringify(data));
console.info(data);
               $.ajax({
                   url: "/record/insert", //url
                   type: "post", //请求类型 ,
                   async:true,
                   contentType:"application/json",//这个是关键

                  // content-type : "application/json",
                  // dataType: "json", //返回的数据类型 ,跟后台配合好 ,一般都是json,
   //data:JSON.stringify(data),
                   data:JSON.stringify(data),
                  success: function(data) {
    console.log(data);
   
},
error:function(){

    alert("登录服务器异常!!!");
}

后台代码:

@PostMapping("insert")
@ResponseBody
public AjaxResult insert(@RequestBody Record record)//@RequestBody是关键
{
    return toAjax(recordService.insert(record));
}
Record类:
public class Record
{
    private static final long serialVersionUID = 1L;

    /** 主键 */
    private Long id;

    /** 调查问卷表id */
    private Long naireId;

    /** 微信的openId */
    private String openId;

    /** 开始答题时间 */
    private Date startTime;

    /** 结束答题时间 */
    private Date endTime;

    /** 手机号 */
    private String telephone;

    /** 姓名 */
    private String name;
    private List answerRecordList;

}

AnswerRecord类:

public class AnswerRecord
{
    private static final long serialVersionUID = 1L;

    /** 主键 */
    private Long id;

    /** 问卷记录表id */
    private Long recordId;

    /** 问题表id */
    private Long questionId;

    /** 选项表id */
    private Long optionId;

    /** 答案 */
    private String answer;

}

转载请注明出处

你可能感兴趣的:(前端,jquery,javascript)