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

37次阅读

共计 2252 个字符,预计需要花费 6 分钟才能阅读完成。

前台 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;}

}

第二种方法效果如图所示

正文完
 0