npm install axios
<script src="https://cdn.bootcss.com/axios/0.19.0/axios.min.js"></script>
https://github.com/axios/axios
method 包括 GET,POST,PUT,DELETE
this.$axios({
method: "POST",
url: '/api/xxx',
data: JSON.stringify(curstomerForm),
headers: { "content-type": "application/json" }
}).then((response) => {
this.$message.error('添加失败');
}).catch((error) => {
this.$message.error('添加失败');
});
let requests = [axios.get('/user/12345'), axios.get('/user/12345/permissions')]
axios.all(requests)
.then(axios.spread(function (response1, response2) {
console.log('两个请求都完成了')
}));
axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
var instance = axios.create({
baseURL: 'https://api.example.com'
});
// 在实例已创建后修改默认值
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
响应拦截需要基于实例
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
// 在发送请求之前做些什么
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
// 对响应数据做点什么
return response;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});