vue更新到2.0之後,作者就宣告不再對vue-resource更新,而是推薦的axios,前一段時間用了一下,現在說一下它的基本用法。
本案例給單獨引入vue.js文件的用戶參考。
一、引入axios.js
<script src="../script/axios.min.js"></script>
二、例子
1、 發送一個GET請求
//通過給定的ID來發送請求 axios.get('/user?ID=12345') .then(function(response){ console.log(response); }) .catch(function(err){ console.log(err); }); //以上請求也可以通過這種方式來發送 axios.get('/user',{ params:{ ID:12345 } }) .then(function(response){ console.log(response); }) .catch(function(err){ console.log(err); });
關於get請求的參數傳遞,你既可以直接拼接在路徑中,也可以通過params來傳遞,交互他會幫你轉爲字符串拼接在路徑上
2、 發送一個POST請求
axios.post('/user',{ firstName:'Fred', lastName:'Flintstone' }) .then(function(res){ console.log(res); }) .catch(function(err){ console.log(err); });
本案例給單獨引入vue.js文件的用戶參考。
一、引入axios.js
<script src="../script/axios.min.js"></script>
二、例子
1、 發送一個GET請求
//通過給定的ID來發送請求 axios.get('/user?ID=12345') .then(function(response){ console.log(response); }) .catch(function(err){ console.log(err); }); //以上請求也可以通過這種方式來發送 axios.get('/user',{ params:{ ID:12345 } }) .then(function(response){ console.log(response); }) .catch(function(err){ console.log(err); });
關於get請求的參數傳遞,你既可以直接拼接在路徑中,也可以通過params來傳遞,交互他會幫你轉爲字符串拼接在路徑上
2、 發送一個POST請求
axios.post('/user',{ firstName:'Fred', lastName:'Flintstone' }) .then(function(res){ console.log(res); }) .catch(function(err){ console.log(err); });