js

1.使用jquery发送前台请求给服务器,并显示数据

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success</title>
</head>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script type="text/javascript">function change() {
        $.ajax({
            type : "GET",
            url : "/abc2",
            data : {
                "user": "wangerxiao"
            },
            success : function(result) {
                alert(result)
            }
        });
    }
</script>
<body>
<h1>this is success page</h1>
<h2>${msg}</h2>
<div class="form" style=" 100px;height: 100px;background-color: red" onclick="change()">
</div>
</body>
</html>

2.后台接受前台请求,并发送相应

@Controller
public class HelloController {
    @GetMapping("/abc2")
    @ResponseBody
    public Map<String,Object> hello2(@RequestParam("user")String user){
        System.out.println(user);
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        Map<String,Object> map = new HashMap<>();
        map.put("1","王二小");
        map.put("2","刘明");
        map.put("3","张三");
        return map;
    }
}

原文地址:https://www.cnblogs.com/20158424-hxlz/p/10407208.html