-
第一步安装axios
npm install axios --save
bower install axios --save
-
引用
直接script标签引用
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
因为axios不是vue得插件,所以再组件中引入
import axios from 'axios'
axios.get();
也可以再main.js中设置成全局的,然后再组件中通过this调用
Vue.prototype.$axios = axios;
this.$axios.get();
-
举一个简单的小栗子
-
GET请求
xingkong() {
axios.defaults.withCredentials = false; //用于解决跨域问题
axios({ method: "get", //请求方式
url: "http://api.estar.net.cn/api/saveEffect", //请求地址
params: {name: 'hehe',age: "20",id: "111",price: "1" } //请求参数
})
.then(response => {console.log(response.data);})
.catch(function(error) { console.log(error); });
},
-
POST请求
xingkong() {
axios.defaults.withCredentials = false; //用于解决跨域问题
axios({ method: "post", //请求方式
url: "http://api.estar.net.cn/api/saveEffect", //请求地址
data: {name: 'hehe',age: "20",id: "111",price: "1" } //请求参数
})
.then(response => {console.log(response.data);})
.catch(function(error) { console.log(error); });
},