springBoot前后端联调demo

1.后端 

package com.yangho.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.yangho.Domain.Result;
import com.yangho.Domain.Role;
import com.yangho.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @ClassName RoleController
 * @Description
 * @Author Yanghao2
 * @Date 2022/2/1 15:45
 * @Version V1.0
 */
@RestController
/*@CrossOrigin  //全部域名放行*/

@RequestMapping("/roles"
)public class RoleController {

    @Autowired
    private RoleService roleService;

    @CrossOrigin  //指定方法放行
    @GetMapping
    public Result findAll() {

        return new Result(true,roleService.findAll());
    }

    @PostMapping
    public Result insert(@RequestBody(required = false) Role role) {

        return new Result(roleService.insert(role));
    }

    @PutMapping
    public Result update(@RequestBody Role role) {
        return new Result(roleService.updateByEntry(role));
    }
}

2.前端


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SpringBoot前后端联调测试</title>

</head>
<body>
<script src="../static/js/Axios.js"></script>
<script src="../static/js/Vue.js"></script>
<div id="app">
    <input type="submit" v-on:click = "getAll" value="查询全部" >
    {{ message }}
    <table border="1" cellspacing="0" align="center">
        <thead align="center" >
        <td>序号</td>
        <td>姓名</td>
        <td>简介</td>
        </thead>
        <tr v-for = "(item,index) in roleArr" align="center">
            <td>{{ item.id }}</td>
            <td>{{item.roleName}}</td>
            <td>{{item.roleDesc}}</td>
        </tr>
    </table>
</div>

<script>

    var app = new Vue({
        el:'#app',
        data:{
            message:"你好!111",
            roleArr :[]
        },
        methods: {
            getAll:function () {
                alert("!!!")
                that = this
                axios.get("http://localhost:8080/roles").then((Result) =>{
                    console.log(Result)
                    var i = -1
                    for (var role of Result.data.data) {
                        i++
                        console.log(role)
                        /*this.roleArr.push[i]({id,roleName,roleDesc})
                        this.roleArr.id = role.id
                        this.roleArr.roleName = role.roleName
                        this.roleArr.roleDesc = role.roleDesc*/
                        this.roleArr.push(role)

                        }



                    console.log(this.roleArr)
                }),function (err) {
                    console.log(err)
                }
            }
        }
    })

</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值