码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Vue项目使用axios配置请求拦截和响应拦截以及判断请求超时处理提示


     

      哈喽大家好啊,最近做Vue项目看到axios

      axios官网:起步 | Axios 中文文档 | Axios 中文网 (axios-http.cn)​​​​​​

      重要点:

             axios是基于Promise封装的

             axios能拦截请求和响应

             axios能自动转换成json数据

            等等

    安装:

    $ npm install axios

    1. Vue项目中使用axios实现请求拦截

    1. import axios from 'axios';// 引入axios
    2. const httpAxios = axios.create({});// 创建实例
    3. let config = {
    4. TIMEOUT: 600000,//设置超时时间为10min
    5. };
    6. // axios 配置超时时间
    7. httpAxios.defaults.timeout = config.TIMEOUT;
    8. // axios设置 请求拦截
    9. httpAxios.interceptors.request.use(
    10. // config配置选项
    11. config => {
    12. console.log(config,'1')
    13. return config;
    14. },
    15. // error
    16. error => {
    17. return Promise.reject(error);
    18. }
    19. )
    1. Vue项目中使用axios实现响应拦截

    1. // axios响应拦截
    2. httpAxios.interceptors.response.use(
    3. // response响应成功
    4. response => {
    5. const config = response.config;
    6. console.log(config,'2')
    7. return response;
    8. },
    9. // 响应error
    10. error => {
    11. const config = error.config;
    12. console.log(config,'3')
    13. if(error.message.includes('timeout')) {
    14. return Promise.reject('timeout');// reject这个错误信息
    15. // 判断请求异常信息中是否含有超时timeout字符串
    16. }
    17. return Promise.reject('网络链接失败,请稍后再试!')
    18. }
    19. )
    1. 封装axios请求

    1. export const getHttpInfo = function (data) {
    2. return new Promise((resolve, reject) => {
    3. let token = ''
    4. if (data.headers) {
    5. token = data.headers.Authorization
    6. }
    7. httpAxios(data).then((res) => {
    8. resolve(res)
    9. }).catch((e) => {})
    10. })
    11. }
    1. 设置超时时间并在响应拦截中判断超时并提示

    1. gethttpInfo({
    2. method: 'post',
    3. url: url,
    4. data: this.order,
    5. headers: {
    6. 'Authorization': localStorage.getItem('token')
    7. }
    8. }).then((res) => {
    9. }).catch((error) => {
    10. this.$message({
    11. type: 'warning',
    12. message: error!=='timeout' ? error : '其他错误'
    13. })
    14. });

    参考原文:

    Vue项目请求超时处理_vue接口请求超时处理_一捆铁树枝的博客-CSDN博客

  • 相关阅读:
    Linux 建立链接文件:ln
    SpringCloud-Nacos
    基于对比学习的NN语言模型训练方法
    [1]-基于图搜索的路径规划基础
    使用vim对比两个文件
    dbeaver 1064 42000 错误 query execution failed
    【项目管理】--敏捷开发管理之Scrum
    ThreadLocal介绍和应用
    python中的数据分析(juypter)
    StoneDB社区答疑第一期
  • 原文地址:https://blog.csdn.net/m0_59157023/article/details/132740105
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号