Controller中怎么弹出layui的警示框
在Controller中弹出layui的警示框,可以通过返回一个特定格式的JSON数据给前端,前端再根据这个数据来弹出警示框。
以下是一个示例:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping("/showAlert")
public String showAlert() {
// 构建返回给前端的JSON数据
String json = "{\"code\": 1, \"msg\": \"弹出警示框提示信息\"}";
return json;
}
}
在前端页面中,可以使用layui的弹框组件layer来显示警示框。示例代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>示例页面</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.6/css/layui.min.css">
</head>
<body>
<button id="btn">点击弹出警示框</button>
<script src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script>
<script>
$(function() {
// 点击按钮时发送Ajax请求
$("#btn").click(function() {
$.ajax({
url: "/showAlert",
success: function(data) {
// 根据返回的JSON数据判断是否弹出警示框
if (data.code === 1) {
layer.alert(data.msg);
}
}
});
});
});
</script>
</body>
</html>
在上述示例中,点击按钮时会发送Ajax请求到/showAlert接口,接口返回的JSON数据中包含code和msg字段。如果code为1,则表示需要弹出警示框,此时使用layer.alert()方法来显示警示框,警示框的内容为msg字段的值
原文地址: http://www.cveoy.top/t/topic/h1XU 著作权归作者所有。请勿转载和采集!