场景
前端通过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", //Controller层的注解方法
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") //Controller层的注解方法
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;
}