vue Axios 使用及注意點
阿新 • • 發佈:2018-11-22
1、使用npm安裝
npm install axios
使用 cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
2、引用
import Axios from 'axios' //引用
Vue.prototype.$axios=Axios; //將Axios掛到Vue原型上
3、GET請求
this.$axios.get("http://www.wwtliu.com/sxtstu/blueberrypai/getIndexBanner.php",{ params:{ type:'引數' } }) .then(res=>{ consoe.log(res) }) .catch(error=>{ console.log(error) })
4、post請求
this.$axios.post("http://www.wwtliu.com/sxtstu/blueberrypai/getIndexBanner.php",{
typs:'引數'
}
)
.then(res=>{
console.log(res)
})
.catch(error=>{
console.log(error)
})
post請求注意:如果後臺接收的資料格式是 form-data格式 : ?name=a&age=20 你需要進行轉換
預設是 x-www-form-urlencoded格式: {name:'a',age:'20'} 這種格式的
怎樣轉換 :node給我們提供了一個庫 qs 我們先將qs引進來
import Qs from 'qs'
然後:Qs.stringify({轉換引數})
this.$axios.post("http://www.wwtliu.com/sxtstu/blueberrypai/getIndexBanner.php",Qs.stringify( { typs:'引數' } )) .then(res=>{ this.msg=res.data.banner }) .catch(error=>{ console.log(error) });