引入文件
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
代碼塊:
1、get請求:
1 var params = { 2 locale: 1, 3 }; 4 // 向具有指定ID的用戶發出請求 5 axios.get('/user?ID=12345') 6 .then(function (response) { 7 console.log(response); 8 }) 9 .catch(function (error) { 10 console.log(error); 11 }); 12 13 14 // 也可以通過 params 對象傳遞參數 15 axios.get('/user',{params:params}).then(function (res) { 16 console.log(res) 17 }).catch(function (err) { 18 console.log(err); 19 })
2、post請求:
1 axios.post('/url', { 2 sex: '1', 3 age: '2' 4 }) 5 .then(function (res) { 6 console.log(res); 7 }) 8 .catch(function (err) { 9 console.log(err); 10 });
3、過個並發請求:
1 function getOne() { 2 return $http.get('/url/1'); 3 } 4 5 function getSecond() { 6 return $http.get('/url/2/secondUrl'); 7 } 8 9 axios.all([getOne(), getSecond()]) 10 .then(axios.spread(function (success, perms) { 11 //兩個請求現已完成 12 }));