axios用法
来源:互联网 发布:数据挖掘入门与实战 编辑:程序博客网 时间:2024/06/07 01:01
1. 设置默认头信息
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
2. 创建请求拦截器 (POST请求配合QS)
// http request 拦截器axios.interceptors.request.use(config => { if (config.method === 'post') { config.data = qs.stringify(config.data, {arrayFormat: 'brackets'}) } return config})
3. 创建响应拦截器 (使用Element UI提示错误信息)
// http response 拦截器 与后台提前定义正确code 错误的提示axios.interceptors.response.use( response => { if (response.data.code === '200') { return response.data.data } else if (response.config.responseType === 'blob') { return response } else { Message.error(response.data.msg) return Promise.reject(response.data.msg) } }, error => { if (error.response) { switch (error.response.status) { case 401: // 返回 401 跳转到登录页面 router.push({ path: '/login' }) break case 500: router.push({ path: '/error/500' }) break case 404: router.push({ path: '/error/404' }) break case 504: router.push({ path: '/error/504' }) break } } return Promise.reject(error.response.data) })
4. 使用get/post请求
// get请求axios.get(`${base}/user/updatePwd`, {params: params})// post请求axios.post(`${base}/login`, params)
5. 使用axios post下载excel
axios.post(`${base}/auth/export`, params, { responseType: 'blob' }).then(res => { console.log(res) if (res.data) { let blob = res.data let a = document.createElement('a') let url = window.URL.createObjectURL(blob) //后台返回的名称 let filename = decodeURI(res.headers['content-disposition'].split('=')[1].split('.')[0]) a.href = url a.download = filename a.click() window.URL.revokeObjectURL(url) } })
阅读全文
0 0
- axios用法
- axios基本用法
- Axios基本用法
- axios基本用法
- vue axios用法
- axios基本用法
- Axios基本用法
- axios基本用法
- axios的一些常见用法
- Axios
- axios
- axios
- axios
- Axios
- axios
- axios
- axios
- axios
- 兄弟连学Python(6)--装饰器的简单认识
- Spring依赖注入static静态变量相关问题
- 数据结构实验之二叉树五:层序遍历
- Zookeeper TTL机制
- Vue(二) 传递对象到子组件中
- axios用法
- 数据结构实验之二叉树六:哈夫曼编码
- redis 多数据库
- 数据结构实验之二叉树七:叶子问题
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 使用python理解快速排序算法
- 对*(char *)0=0的一点分析(zigbee协议栈中的)
- 【八中】7数码
- es-jvm.options范本