网络应用
Vue.js 2.0 版本推荐使用 axios  来完成 ajax 请求。
 axios基本使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
   | <!DOCTYPE html> <html lang="en">
  <head>     <meta charset="UTF-8" />     <meta name="viewport" content="width=device-width, initial-scale=1.0" />     <meta http-equiv="X-UA-Compatible" content="ie=edge" />     <title>axios基本使用</title> </head>
  <body>     <input type="button" value="get请求" class="get">     <input type="button" value="post请求" class="post">          <script src="https://unpkg.com/axios/dist/axios.min.js"></script>     <script>         
 
 
 
 
 
          document.querySelector(".get").onclick = function () {             axios.get("https://autumnfish.cn/api/joke/list?num=6")                          .then(function (response) {                 console.log(response);               },function(err){                   console.log(err);               })         }         
 
 
 
 
 
          document.querySelector(".post").onclick = function () {             axios.post("https://autumnfish.cn/api/user/reg",{username:"盐焗西兰花"})             .then(function(response){                 console.log(response);                 console.log(this.skill);             },function (err) {                 console.log(err);               })           }
      </script> </body>
  </html>
   | 
 
- axios必须先导入才可以使用
 
- 使用get或post方法即可发送对应的请求
 
- then方法中的回调函数会在请求成功或失败时触发
 
- 通过回调函数的形参可以获取响应内容,或错误信息
 
 axios+vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
   | <!DOCTYPE html> <html lang="en">
  <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <meta http-equiv="X-UA-Compatible" content="ie=edge">     <title>axios+vue</title> </head>
  <body>     <div id="app">         <input type="button" value="获取笑话" @click="getJoke">         <p> {{ joke }}</p>     </div>          <script src="https://unpkg.com/axios/dist/axios.min.js"></script>          <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>     <script>         
 
 
 
 
 
          var app = new Vue({             el:"#app",             data:{                 joke:"很好笑的笑话"             },             methods: {                 getJoke:function(){                                          var that = this;                     axios.get("https://autumnfish.cn/api/joke").then(function(response){                                                  console.log(response.data);                                                  that.joke = response.data;                     },function (err) {  })                 }             },         })
      </script> </body>
  </html>
   | 
 
- axios回调函数中的this已经改变,无法访问到data中数据
 
- 把this保存起来,回调函数中直接使用保存的this即可
 
- 和本地应用的最大区别就是改变了数据来源