通过查询,让查询的结果显示到同一个页面,不刷新

controller层 通过接口查询查询到的数据 id,name,两条数据,

img

img

前端, 我如何修改前端,才能吧获取到数据展现出来


<body>
<form action="" method="post" id="bgcha">
    查询<input type="text" name="zdid" style="width: 200px; height: 25px;margin-left: 20px" id="zdid">
    <button type="submit" class="layui-btn layui-btn-warm layui-btn-radius" onclick="cha()">
        <p style="margin-top: -3px;color: black;font-weight: bold">查询</p></button>
    查询结果显示<input type="text"  style="width: 200px; height: 25px;margin-left: 20px">
    查询name显示<input type="text"  style="width: 200px; height: 25px;margin-left: 20px">
</form>
</body>
<script src="../jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script>
    function cha() {
        $.ajax({
            type:'post',
            url:'/zd/cha',
            data:$('#bgcha').serialize(),
            success:function (data) {
                    alert("123");
            }
        })
    }
</script>

你把数据绑定到model中了返回的是个页面地址,加上这个注解@ResponseBody 是返回JSON数据,可是你反返回的是个页面地址吧,这个应该返回zongduan这个数据,这样是json 数据,在ajax中的success:function (data) 中的data就是你返回的数据,使用$(".class").value 更新下页面中的显示就行了,这样才能不刷新显示
有帮助,望采纳

你controller中return返回的要是一个list,然后list包含id 和name,然后前端取list的值就可以了,


```java
 @RequestMapping("/account/findAll")
    public String findAll(Model model){  //存数据, Model对象
        System.out.println("Controller表现层:查询所有账户...");
        // 调用service的方法
        List<Account> list = accountService.findAll();
        model.addAttribute("list",list);
        return "list";
    }

```

比如这样的