npm install axios --save
http://123.207.32.32:8000/home/multidata
// 导入 axios
import axios from "axios";
// 使用
axios({
url:'http://123.207.32.32:8000/home/multidata'
}).then(res => {
console.log(res);
})
axios({
url:'http://123.207.32.32:8000/home/data',
params:{
type:'pop',
page:1
}
}).then(res => {
console.log(res);
})
/**
* 发送多请求
*/
axios.all([
axios({
url:'http://123.207.32.32:8000/home/multidata'
}),
axios({
url:'http://123.207.32.32:8000/home/multidata'
})
]).then(res => {
// 返回结果res是一个数组 [0] 就是第一个请求返回的结果 [1]...
console.log(res);
})
axios.all([
axios({
url:'http://123.207.32.32:8000/home/multidata'
}),
axios({
url:'http://123.207.32.32:8000/home/multidata'
})
// 可以通过 axios.spread展开返回结果
]).then(axios.spread((res1,res2) => {
// 返回结果res是一个数组 [0] 就是第一个请求返回的结果 [1]...
console.log(res1);
console.log(res2);
}))
/**
* 默认配置
*/
// 默认前缀URL
axios.defaults.baseURL = 'http://123.207.32.32:8000'
// 超时时间 单位:毫秒
axios.defaults.timeout = 5000
/**
* 简单使用
*/
axios({
url:'/home/multidata'
}).then(res => {
console.log(res);
})
let config = {
baseURL:'http://123.207.32.32:8000',
timeout:5000
};
let axiosInstance = axios.create(config);
axiosInstance({
url:'/home/multidata'
}).then(res => {
console.log(res);
})
let config = {
baseURL:'http://123.207.32.32:8000',
timeout:5000
};
let axiosInstance = axios.create(config);
// 请求拦截器
axiosInstance.interceptors.request.use(
// 拦截请求时的 config
config => {
console.log(config);
return config;
},
// 拦截请求失败的error 一般请求不会失败的
error => {
}
)
// 响应拦截器
axiosInstance.interceptors.response.use(
// 请求返回的数据
res => {
console.log(res);
// 做数据过滤 只返回后端返回的data
return res.data;
},
// 请求失败的error
error => {
console.log(error);
}
)