Vue ajax (axios)

axios-0.18.0.js可以导入jar

<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>axios</artifactId>
    <version>0.18.0</version>
</dependency>

vuejs-2.5.16.js可以导入

dependency>
    <groupId>org.webjars.bowergithub.vuejs</groupId>
    <artifactId>vue</artifactId>
    <version>2.5.16</version>
</dependency>
 <script src="webjars/vue/dist/vue.min.js"></script>
  <script src="webjars/axios/0.18.0/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
    <table border="1">
        <tr>
            <td>{{name}}</td>
        </tr>
        <tr v-for="u in userList">
            <td>{{u.id}}</td>
            <td>{{u.username}}</td>
            <td>{{u.password}}</td>
            <td>{{u.sex}}</td>
            <td>{{u.age}}</td>
            <td>{{u.email}}</td>
        </tr>
    </table>
</div>
<script>
    new Vue({
        el:'#app',
        data:{
            name:"wjc",
            user: {id:"",username:"aaa",password:"",age:"",sex:"",email:""},
            userList:[]
        },
        methods:{
            //查询所有
            findAll:function () {
                var _this=this;
                axios.post('http://localhost:8080/findAll').then(function (response) {
                        _this.userList = response.data;//响应数据给userList赋值
                        console.log( _this.userList);
                    })
                    .catch(function (error) {
                        console.log(error);
                    })
            }
        },
         findById:function (userid) {
            //在当前方法中定义一个变量,表明是vue对象
            var _this = this;
            axios.get('http://localhost:8080/findById',{params:{id:userid}})
                .then(function (response) {
                    _this.user = response.data;//响应数据给userList赋值
                    $("#myModal").modal("show");
                })
                .catch(function (error) {
                    console.log(error);
                })
        },
        update:function (user) {//post请求
            //在当前方法中定义一个变量,表明是vue对象
            var _this = this;
            axios.post('http://localhost:8080/updateUser', _this.user)
                .then(function (response) {
                    _this.findAll();
                })
                .catch(function (error) {
                    console.log(error);
                });
        }
        created:function() {//当我们页面加载的时候触发请求,查询所有
            this.findAll();
        }
    })
</script>
</body>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值