springmvc使用ajax前后端交互的四种形式

若要后台返回对象格式的数据,需要在pom.xml中引入(版本太低会报错)

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.6</version>
</dependency>

controller层代码

@Controller
public class TestController {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(){
        return "test";
    }


    @RequestMapping(value = "/post/aaa",method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView aaa(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());

        System.out.println(username);
        System.out.println(password);
        mv.addObject("data","chenggong");

        return mv;
    }
    @RequestMapping(value = "/post/bbb")
    @ResponseBody
    public String bbb(HttpServletRequest request, HttpServletResponse response) {
        System.out.println(request.getParameter("username"));
        System.out.println(request.getParameter("password"));

        request.setAttribute("data","ppp");
        return "String";
    }

    @RequestMapping(value = "/post/ccc")
    @ResponseBody
    public Map<String, Object> ccc(@RequestParam Map<String,String> map) {
        System.out.println(map.get("username"));
        System.out.println(map.get("password"));
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("jieguo","chenggong");
        return resultMap;
    }
    @RequestMapping(value = "/post/ddd")
    @ResponseBody
    public void ddd(User user, HttpServletResponse response) throws IOException {
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        response.getWriter().print("helloworld");
    }




    @RequestMapping("/post/list")
    @ResponseBody
    public List<User> List(){
        List<User> list = new ArrayList<>();
        User u = new User();
        u.setUsername("qqq");
        u.setPassword("eee");
        list.add(u);
        return  list;
    }


}

前端代码

<html>
<head>
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>

hello
<input type="button" value="aaa" οnclick="aaa()">
<input type="button" value="bbb" οnclick="bbb()">
<input type="button" value="ccc" οnclick="ccc()">
<input type="button" value="ddd" οnclick="ddd()">
<input type="button" value="eee" οnclick="eee()">
<script>
    function aaa() {
        var field = {"username":"aaa","password":"bbb"};
        $.ajax({
            url:"./post/aaa",
            datatype:"application/json",
            data:field,
            type:"post",
            success:function (data) {
                debugger;
            }
        })
    }
    function bbb(){
        var field = {"username":"aaa","password":"bbb"};
        $.ajax({
            url:"./post/bbb",
            datatype:"json",
            data:field,
            type:"post",
            success:function (data) {
                debugger;
            }
        })
    }
    function ccc(){
        $.ajax({
            url:'./post/ccc',
            type:'post',
            dataType:'json',
            data:{"username":"aaa","password":"bbb"},
            success:function(data){
                debugger;
            }
        });

    }
    function ddd(){
        $.ajax({
            url:"./post/ddd",
            type:"post",
            datatype:"application/json",
            data:{"username":"aaa","password":"bbb"},
            success:function (result) {
                debugger;
            }
        });
    }


    function eee(){
        $.ajax({
            url: "./post/list",//请求地址
            type: "POST",
            dataType: "json",
            success: function(data) {
                debugger;
            }
        });
    }


</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值