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

效果

相关推荐
渣渣盟15 小时前
Spark 性能调优实战:从开发到生产落地
javascript·ajax·spark
ID_180079054731 天前
Python 实现亚马逊商品详情 API 数据准确性校验(极简可用 + JSON 参考)
java·python·json
代码AI弗森1 天前
配置文件战争:TOML/YAML/JSON 为何成为不同框架的“专属标配”?
json
Bechamz2 天前
大数据开发学习Day23
大数据·学习·ajax
Cyan_RA92 天前
SpringMVC REST 详解
java·spring·mvc·springmvc·restful·jquery·jsp
lifewange2 天前
CNode API v1 完整接口文档(JSON 规范整理)
java·前端·json
测试修炼手册2 天前
[测试技术] 深入理解 JSON Web Token (JWT)
前端·json
九转成圣2 天前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
小袁拒绝摆烂2 天前
多表关联大平层转JSON树形结构
java·json
学术阿凡提3 天前
Spring Boot 集成 Fastjson2 完整教程:从入门到避坑
spring boot·安全·json