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

效果

相关推荐
Philtell12 小时前
Ubuntu22.04TLS VS Code配置setting.json
json
stella·13 小时前
后端二进制文件,现代前端如何下载
前端·ajax·状态模式·axios·request·buffer·download
REDcker15 小时前
AIGCJson 库介绍与使用指南
c++·json·aigc·c
lingzhilab15 小时前
零知IDE——零知ESP32 + INA219电流传感器实现18650锂电池智能充放电监测系统
ide·单片机·json
全栈前端老曹15 小时前
【包管理】npm最常见的10大问题故障和解决方案
前端·javascript·rust·npm·node.js·json·最佳实践
韩师学子--小倪1 天前
fastjson与gson的toString差异
java·json
极客先躯2 天前
基于 EasyUI 和 jQuery 封装的企业级日期时间选择器组件库,采用标准的 jQuery 插件开发规范,实现了高度模块化和可复用的 UI 组件
ui·jquery·日期选择器·时间选择器·easyui
深蓝电商API2 天前
Scrapy爬取Ajax动态加载页面三种实用方法
爬虫·python·scrapy·ajax
牛马1112 天前
ios swift处理json数据
ios·json·swift
Wpa.wk2 天前
性能测试工具 - JMeter工具组件介绍二
运维·经验分享·测试工具·jmeter·自动化·json