SpringBoot对Ajax的应用

package com.circle.controller;

import com.circle.entity.UserBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class JsonController {
    @Resource
    private UserBean userBean;

    @PostMapping("/userBean/{name}/{age}/{gender}")
    public UserBean getUserBean(@PathVariable("name") String name,
                                @PathVariable("age") int age,
                                @PathVariable("gender") String gender){
        userBean.setName(name);
        userBean.setAge(age);
        userBean.setGender(gender);
        return userBean;
    }

    @GetMapping("/userBean")
    public UserBean getUserBean(UserBean userBean){
        return userBean;
    }
}
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>ajax</title>
    <script src="/script/jquery-3.2.1.min.js"></script>
</head>
<body>

<script>
    $(function () {
        $.post("/circle/userBean/张三/20/男", {}, function (result) {
            console.log(result)
            console.log(result.name)
            console.log(result.age)
            console.log(result.gender)
        })

        $.get("/circle/userBean", {name: "李四", age: 15, gender: "女"}, function (result) {
            console.log(result)
            console.log(result.name)
            console.log(result.age)
            console.log(result.gender)
        })
    })
</script>
</body>
</html>
posted @   王^.^令  阅读(210)  评论(0)    收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示