前端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>
你今天的努力,只是为了让你的苦更有价值