axios
axios的 请求方式
get()
post()
delete()
put()
patch()
- get() 请求和 delete()请求 用法一样
axios.get(url, {
params:{
key:value
},{
// 配置项 config
}
}).then(res => {
})
axios.delete(url, {
params:{
key:value
},
{
// 配置项 config
}
}).then(res => {
})
- post() put() patch() 用法类似
axios.post(url, {
key:value}, {
config}).then(res => {
})
- axios的综合用法
axios({
url:url,
method:'',
...
}).then(res => {
})
axios(url, {
method:'',
...
}).then(res => {
})
axios 拦截器
拦截器:
发送请求时进行拦截, 统一处理我们的请求(比如权限验证)
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
// 在发送请求之前做些什么
// 如果添加数据
config.params.key = value // 通过params 添加数据
config.data.key = value // 通过data 添加数据
config.headers.key = value // 通过headers 添加数据
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
// 添加响应拦截器
// 主要针对请求失败的错误处理
axios.interceptors.response.use(function (response) {
// 对响应数据做点什么
return response;
}, function (error) {
// 对响应错误做点什么
// error 是一个对象 通过 [error] 方式打印error可以查看error中的属性
return Promise.reject(error);
});