欢迎光临
我们一直在努力

Spring如何使用RequestBody接收ajax传递参数

代码清单1

$.ajax({
        url: context + "/service/datareport/echartScore",
        async: false,
        dataType: "json",
        data: JSON.stringify({"srYear": $('#srYear').val(), 'srType': $('#srType').val(), 'inspectionTeam': $('#inspectionTeam').val()}),
        type: "POST",
        contentType:'application/json;charset=UTF-8',
        success: function (countData) {
            $.each(countData.data, function (index, value) {
                if (index < 5) {
                    xData.push(value.UNIT_NAME);
                    yDsData.push(value.SCORE);
                }
            })
        }
    });

注意的地方是:contentTypetypedata

代码清单2

@RequestMapping("/echartScore")
@ResponseBody
public Map echartScore(@RequestBody Map<String, Object> parameters) {
    List<Map> evaluateList;
    if (!ObjectUtils.isEmpty(parameters.get("inspectionTeam"))) {
        Context context = ContextFactory.getContext();
        List<Map> permitList = context.getUserDataPermit((String) parameters.get("inspectionTeam"));
        List<String> permitUnitList = permitList.stream().map(s -> (String) s.get("organId")).collect(Collectors.toList());
        parameters.put("unitId", permitUnitList);
    }
    if (StringUtils.equals("0", (String) parameters.get("srType")) || StringUtils.equals("1", (String) parameters.get("srType"))) {
        evaluateList = evaluateService.queryUnitScoreByType(parameters);
    } else {
        evaluateList = evaluateService.queryUnitScore(parameters);
    }
    Map map = new HashMap();
    int total = PageUtil.getTotalCount();
    map.put("total", total != -1 ? total : evaluateList.size());
    map.put("data", evaluateList);
    return map;
}

参考:

  1. Ajax+SpringMVC requestBody
赞(0) 打赏
未经允许不得转载:Ddmit » Spring如何使用RequestBody接收ajax传递参数

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

登录

找回密码

注册