前台AJAX传数组,后台的java接收

1

前台AJAX传数组,后台的java接收(后台接收前端发送的数组类型数据)两种解决方法

第一种方法,前端将数组通过JSON.stringify()方法转换为json格式数据,后台将接收的json数据转换为数组

function search() {
            var equiNames = JSON.stringify($("#equiNames").val());
            var startDate = $('#daterange-btn span').text().substring(0, 10);
            var endDate = $('#daterange-btn span').text().substring(13);
            $.ajax({
                url : "dataAcquisition/report",
                type : "post",
                dataType : "json",
                data : {
                    "equiNames" : equiNames,
                    "startDate" : startDate,
                    "endDate" : endDate
                },
                success : function(result) {
                    ……
                    }
                }
            });
        }
@RequestMapping("/report")
    public void report(String equiNames, String startDate, String endDate, HttpServletRequest request,
            HttpServletResponse response) throws ExecutionException, InterruptedException, IOException, ParseException {
        //将接收的json数据转换为数组
        List<String> equiNameList = new Gson().fromJson(equiNames, new TypeToken<List<String>>() {
        }.getType());
        List<DataAcquisitionVo> resultList = dataAcquisitionService.report(equiNameList, startDate, endDate);
        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().write(new Gson().toJson(resultList));
    }

第二种方法,前端通过设置traditional属性为true直接传递数组 */,后台通过对象接收

function search() {
            var equiNames = JSON.stringify($("#equiNames").val());
            var startDate = $('#daterange-btn span').text().substring(0, 10);
            var endDate = $('#daterange-btn span').text().substring(13);
            $.ajax({
                url : "dataAcquisition/report",
                type : "post",
                dataType : "json",
                traditional : true,//用传统方式序列化数据
                data : {
                    "equiNames" : equiNames,
                    "startDate" : startDate,
                    "endDate" : endDate
                },
                success : function(result) {
                    ……
                    }
                }
            });
        }

对象

@RequestMapping("/report")
    public void report(ReportParaVo rp, HttpServletRequest request, HttpServletResponse response)
            throws ExecutionException, InterruptedException, IOException, ParseException {
        
        List<DataAcquisitionVo> resultList = dataAcquisitionService.report(rp);
        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().write(new Gson().toJson(resultList));
    }
import java.util.List;

public class ReportParaVo {
    private List<String> equiNames;
    private String startDate;
    private String endDate;

    public List<String> getEquiNames() {
        return equiNames;
    }

    public void setEquiNames(List<String> equiNames) {
        this.equiNames = equiNames;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

}

第二种方法效果如图所示

你可能感兴趣的

载入中...