1.npm下载安装
$ npm install axios
2.在main.js中引入该模块并挂载到vue原型上
import Axios from 'axios'
Vue.prototype.$axios = Axios
3.组件中使用axios
post:
<script>
import qs from 'qs'
export default {
name: "posts",
data(){
return{
postData:[]
}
},
//post请求的参数必须为x-www-form-urlencoded,所以需要引入第三方qs库,通过qs.stringify的方式将form-data格式的值转为x-www-form-urlencoded,如果不转换,则将参数直接以form-data的格式拼接在接口连接后面。
created(){
this.$axios.post("http://www.wwtliu.com/sxtstu/blueberrypai/login.php",qs.stringify({
user_id:""
})).then(res=>{
console.log(res);
}).catch(error=>{
console.log(error);
})
}
}
</script>
get:(测试阶段需解决跨域问题)
<script>
export default {
name: "doubanAxios",
data(){
return{
doubanData:[]
}
},
created(){
var url = this.HOST + '/v2/movie/coming?apikey=0df993c66c0c636e29ecbb5344252a4a';
this.$axios({
method:'get',
url:url,
}).then(res=>{
console.log(res);
}).catch(error=>{
console.log(error);
});
}
}
</script>
网友评论