1、axios的安装
A、npm安装
npm install axios --save
B、 直接利用cdn引入
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
2、数据请求
A、GET请求
带参数:
axios.get(url').then(res=>{
console.log(res);
....
}).catch(error=>console.log(error));
不带参数:
axios.get('url',{params:{id:123}}).then(res=>{
console.log(res);
....
}).catch(error=>console.log(error));
B、POST请求
axios.post('url', {
param1: '123',
param2: '456'
}).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
3、跨域请求
设置代理(在config->index.js)中
// 配置代理
proxyTable: {
'/api':{ // api为匹配项
target:'http://localhost:8081/', // 设置代理目标
changeOrigin: true,
pathRewrite: { // 重写路径
'^/api': '/'
}
}
},
//请求数据
import axios from 'axios';
export default {
name: 'app',
getData(){
axios.get('/api/data').then(res=>{
console.log('getData:',res);
})
}
}