axios封装
来源:互联网 发布:网络数据传输结构 编辑:程序博客网 时间:2024/06/05 22:30
使用时基本参数和 jQuery ajax 一样
ajax: function (opt) { let opts = opt || {} if (!opts.url) { this.$Toast.error('请填写接口地址') return false } let headers = opts.headers || { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } if (opts.type === 'post' || opts.type === 'put' || opts.type === 'patch') { headers[csrfHeader] = csrfContent } let options = { method: opts.type || 'get', url: opts.url, headers: headers, // `baseURL` 将自动加在 `url` 前面,除非 `url` 是绝对的,便于传递相对url baseURL: '/app', timeout: opts.time || 10 * 1000, responseType: opts.dataType || 'json', transformRequest: opt.transformRequest || [function (data) { if (opts.type === 'post' || opts.type === 'put' || opts.type === 'patch') { data = Qs.stringify(data) } return data }] } if (opts.type === 'post' || opts.type === 'put' || opts.type === 'patch') { options.data = opts.data || {} } else { options.params = opts.data || {} } // ajax 请求拦截器 axios.interceptors.response.use( response => {return response}, error => { if (error.response) { switch (error.response.status) { case 401: window.location.href = '/login' } } return Promise.reject(error.response.data) } ) axios(options).then(function (res) { if (res.status === 200) { if (opts.success) { opts.success(res.data, res) } } else { if (opts.error) { opts.error(res) } else { this.$Toast.error('服务器出错') } } }).catch(function (error) { if (opts.error) { opts.error(error) } else { this.$Toast.error('服务器出错') } })}
ajaxError: function (res) { let errorMsg if (res.code && res.error.errorMsg) { errorMsg = res.error.errorMsg } else if (typeof res === 'string' && res.indexOf('<html>') !== -1 && res.indexOf('登陆') !== -1) { window.location.href = '/login' } else { errorMsg = '服务器出错' } this.$Toast.error(errorMsg)}
阅读全文
1 0
- axios封装
- axios 封装
- vue实力封装axios
- vue axios 二次封装
- vue 封装 axios
- vue项目封装axios
- vue 插件和axios封装
- nuxt之axios 二次封装
- vue 中axios的封装
- 封装简单的axios库
- 基于axios封装fetch方法及调用
- vue+axios方法封装(restful,ajax)
- axios中对cookie的封装
- vue项目中对axios的封装
- Axios
- axios
- axios
- axios
- 巧用Map实现Java版的left join和group by
- CNN在TensorFlow框架实现
- Linux创建子账户并设置密码
- 第七周项目5---排队看病模拟
- 第六篇: 分布式配置中心(Spring Cloud Config)
- axios封装
- Redis正确使用的十个技巧
- 第888期机器学习日报(2017-02-22)
- 第886期机器学习日报(2017-02-20)
- 第八周(1) 项目5
- 第880期机器学习日报(2017-02-14)
- 第890期机器学习日报(2017-02-24)
- 第889期机器学习日报(2017-02-23)
- Task原理剖析