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

效果

相关推荐
喵本喵叁肆16 小时前
06-M6-部门过滤与综合研判-从问答机到研判助手
前端·javascript·jquery
এ慕ོ冬℘゜18 小时前
jQuery 核心方法
前端·javascript·jquery
CarIise18 小时前
事件传播、CSS动画、服务器字体与jQuery
服务器·css·jquery
江畔柳前堤19 小时前
字节跳动产品全景图:从应用表象到技术深海的七层解剖
人工智能·chatgpt·架构·json·batch
马优晨21 小时前
pom.xml 中 jquery webjars 依赖解释
xml·前端·jquery·jquery webjars·webjars依赖
এ慕ོ冬℘゜1 天前
前端实战:jQuery实现动态表格渲染与状态切换功能
前端·javascript·jquery
7177773 天前
Gitee 用 AI Agent 重塑 DevSecOps 全流程:从 “被调用的工具“ 到 “主动干活的队友“
人工智能·gitee·jquery
光泽雨3 天前
JSON /XML转换
xml·json
晴天163 天前
VS Code `launch.json` 指南(Node.js版)-Day30
node.js·json
XLYcmy4 天前
PDF论文处理器 - 完整使用指南
pdf·llm·json·agent·token·csv·dify