• 前端vue的get和post请求


    vue的get和post需要两个文件vue.js和vue-resource.js

    以下是实现的代码,可以参考一下,需要注意的接口的请求需要考虑跨域的问题,其次就是访问页面需要在tomcat下访问,否则也会报跨域的问题

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
    <meta charset="utf-8" />
    <title>test</title>
    <meta name="author" content="xiewg@cebserv.com" />
    <meta name="copyright" content="www.doyoe.com" />
    <script src="js/vue.js"></script>
    <script src="js/vue-resource.js"></script>
    <script src="js/jquery.min.js"></script>
    <style>
    p~p{color:#f00;}
    </style>
    </head>
    <body>
    <div id="app">
    <table>
    <tr v-for="item in message">
    <td>{{item.expertId}}</td>
    <td>{{item.categoryName}}</td>
    </tr>
    </table>
    <button v-on:click="greet">get</button>
    <button v-on:click="postgreet">post</button>
    </div>
    
    <script type="text/javascript">
    //var books={[]};
    //{
    // [
    //	{"id":1,"author":"曹雪芹","name":"红楼梦","price":32},
    //	{"id":2,"author":"施耐庵","name":"水浒传","price":30},
    //	{"id":"3","author":"罗贯中","name":"三国演义","price":24},
    //	{"id":4,"author":"吴承恩","name":"西游记","price":20}
    //	]
    //}
    new Vue({
    el: '#app',
    data:{
    message: [
    {"id":1,"author":"曹雪芹","name":"红楼梦","price":32},
    {"id":2,"author":"施耐庵","name":"水浒传","price":30},
    {"id":"3","author":"罗贯中","name":"三国演义","price":24},
    {"id":4,"author":"吴承恩","name":"西游记","price":20}
    ]
    },
    created: function () {
    //this.greet();
    },
    ready:function(){
    // this.greet();
    },
    methods:{
    
    greet: function (event) {
    //`this` 在方法里指向当前 Vue 实例
    this.$http.get('http://172.16.1.218:58080/API/app/expertCategory/getQuestionList?expertCategoryId=2').then(function(data){
    //this.books=data.body.data;
    alert(data.body.returnCode);
    this.message=data.body.resultData;
    //this.$set('books', data.body);
    },function(res){
    console.log(res.status);
    });
    //alert('Hello ' + this.book + '!')
    //`event` 是原生 DOM 事件
    // if (event) {
    // alert(event.target.tagName)
    // }
    },
    postgreet: function (event) {
    //`this` 在方法里指向当前 Vue 实例
    this.$http.post('http://localhost:8080/customer/getCustomer',{param:1111}).then(function(data){
    //this.books=data.body.data;
    alert(data.body.message);
    //this.message=data;
    this.greet();
    },function(res){
    console.log(res.status);
    });
    //alert('Hello ' + this.book + '!')
    //`event` 是原生 DOM 事件
    // if (event) {
    // alert(event.target.tagName)
    // }
    },	
    }
    })
    </script>
    </body>
    
    </html>
    

      

  • 相关阅读:
    python实现模拟登录
    python进阶八_警告和异常
    我的软考之路(六)——数据结构与算法(4)之八大排序
    Modern source-to-source transformation with Clang and libTooling
    heibernate增删改查总结一下自己的不足
    hibernate之增删改查demo
    前台之boostrap
    <input value="hidden">的作用
    Active Desktop--桌面字体背景被修改
    中文乱码问题解决方法总结
  • 原文地址:https://www.cnblogs.com/xwgcxk/p/8855682.html
Copyright © 2020-2023  润新知