码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • axios在vue里简单的封装使用


    文章目录

    • 前言
    • 一、axios
      • 1.通过npm命令安装
      • 2.在工具类文件夹utils里新建request.ts文件
      • 3.在新建api文件夹新建index.ts文件创建方法
      • 4.举例使用post方法testData
    • 二、springboot接收axios传送数据


    前言

    本篇主要介绍。


    一、axios

    1.通过npm命令安装

    npm install axios
    
    • 1

    2.在工具类文件夹utils里新建request.ts文件

    baseURL为代理地址,在前一篇文章有配置。将’/urlApi’替代’http://localhost:8060‘跨域。

    // 从axios源码中可知,axios实例的类型为AxiosInstance,响应的数据类型为AxiosResponse,请求需要传入的参数类型为AxiosRequestConfig
    import axios, {AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig} from 'axios';
    const service:AxiosInstance = axios.create({
        baseURL: '/urlApi',
        // baseURL: 'http://localhost:8060',
        timeout: 5000,
        //跨域携带cookie
        withCredentials: true
    });
    
    // 添加请求拦截器
    service.interceptors.request.use(
        (config: AxiosRequestConfig) => {
            return config;
        },
        (error: AxiosError) => {
            console.log(error);
            return Promise.reject();
        }
    );
    
    // 添加响应拦截器
    service.interceptors.response.use(
        (response: AxiosResponse) => {
    
            console.log("response:",response);
            if (response.status === 200) {
                return response;
            } else {
                Promise.reject();
            }
        },
        (error: AxiosError) => {
            console.log(error);
            return Promise.reject();
        }
    );
    
    export default service;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    3.在新建api文件夹新建index.ts文件创建方法

    import request from '../utils/request';
    
    export const fetchData = () => {
        return request({
            url: './table.json',
            method: 'get'
        });
    };
    export const testData = (data: any) => {
        return request({
            url: 'test/vueTest',
            method: 'post',
            data,
        });
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.举例使用post方法testData

    在.vue文件里使用,向后端传输数据data

    // 使用axios通信
    import { onMounted } from 'vue'
    import {
      testData
    } from "../api/index"
    let data = {username: 'admin'};
    onMounted(() => {
      testData(
          data
      ).then(res => {
        console.log('login response data', res)
      })
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    二、springboot接收axios传送数据

    在模块/test下,s所以完整的地址为"http://localhost:4000/urlApi/test/vueTest"。
    @RequestBody接收数据,同时传送username。

    @RequestMapping(value = "/vueTest", method = RequestMethod.POST)
        @ResponseBody
        //
        public String vueTest(HttpSession session, @RequestBody String data){
            System.out.println("后台打印:"+data);
            String username = (String) session.getAttribute("username");
            return username;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    后台接收到成功打印data,axios成功接收username。


  • 相关阅读:
    VsCode includePath设置问题
    恶意代码防范技术笔记(四)
    【Java+SpringBoot】监考任务分配系统(源码+远程部署+项目定制开发+代码讲解+毕业设计+答辩教学)
    Lurk——Recursive zk-SNARKs编程语言
    E-梅莉的市场经济学
    S11-斜线表头操作
    数据库基础
    链表面试题-刷题
    洛谷 P5058 [ZJOI2004]嗅探器(割点)
    店铺营收未达预期,用DataEase开源BI工具分析原因所在
  • 原文地址:https://blog.csdn.net/qq_43881663/article/details/127920087
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号