Jquery中使用ajax传json参数并从SpringBoot后台Controller返回消息

场景

前端通过ajax提交json格式的数据,后台接受到实现具体的业务后

返回给前端消息提示。

实现

html页面代码

复制代码
 <button id="printBtn" class="btn btn-info " type="button"><i class="fa fa-trash-o"></i> 打印</button>

调用js部分代码

复制代码
//打印按钮点击事件
    $("#printBtn").click(function () {
        var data = t.rows(['.selected']).data()[0];
        if(undefined===data){
            swal({
                type: 'warning',
                title: '提示:',
                text: '请首选选择一行数据!',
                confirmButtonColor: "#1ab394",
            })
        }else{
            printCode(data.id);
        }
    });

ajax请求方法

复制代码
function printCode(id){
    debugger
    $.ajax({
        type: 'POST',
        url: "/wmsReceiveOrder/doPrintPostRequest",
        cache: false,  //禁用缓存
        data:JSON.stringify({"id":id}),
        contentType: "application/json",
        dataType: "json",
        success: function (result) {
            debugger
            alert(result.message)
        }
    })
    return false;
}

来到url所对应的后台Controller中的方法

复制代码
@ResponseBody
    @RequestMapping(value = "/doPrintPostRequest")
    public Map<String, Object> doPrintPostRequest(@RequestBody Map<String, Object> params) {
         Map<String, Object> result = new HashMap<String, Object>();
        Object PrintId = params.get("id");
        String paramID="";
        if(PrintId!=null){
           paramID=PrintId.toString();
        }
        result.put("statusCode",  "200");
        result.put("message", "参数是:"+ paramID);
        return result;
    }

效果

相关推荐
ID_180079054739 小时前
小红书笔记详情 API 接口系列 + 标准 JSON 返回参考(完整版)
数据库·笔记·json
小狗丹尼40012 小时前
JSON 基础认知、数据转换与 Flask 前后端交互全解
python·flask·json
_周游1 天前
Kaptcha—Google验证码工具
java·intellij-idea·jquery
奔跑的呱呱牛1 天前
arcgis-to-geojson双向转换工具库
arcgis·json
武超杰1 天前
SpringMVC核心功能详解:从RESTful到JSON数据处理
后端·json·restful
还是大剑师兰特2 天前
Vue3 前端专属配置(VSCode settings.json + .prettierrc)
前端·vscode·json
qq_283720052 天前
Cesium实战(三):加载天地图(影像图,注记图)避坑指南
json·gis·cesium
雷帝木木2 天前
Flutter for OpenHarmony:Flutter 三方库 cbor 构建 IoT 设备的极致压缩防窃协议(基于标准二进制 JSON 表达格式)
网络·物联网·flutter·http·json·harmonyos·鸿蒙
长安11082 天前
JsonCpp的编译与使用
json
凌晨一点的秃头猪2 天前
JSON 文件基础介绍
json