axios请求

2023-05-16

可传参数

axios({
        //请求方式 get post delete put
        method:"",
        //请求地址
        url:"",
        //query 参数
        params:{

        },
        // body参数
        data:{

        },
        //请求头 一般是用来指定请求传参格式
       
        //请求中没有 data参数 所以headers content-type 默认为 content-type: "application/json;charset=UTF-8"
        // headers: {
        //   // 'content-type': 'application/json;charset=UTF-8'(默认)
        //   "Content-Type": "application/x-www-form-urlencoded",
        // 'content-type':"multipart/form-data"
        // },
        
        headers:{},
      }).then(function(res){
        console.log(res)
      })
    }

get请求

query

如何区分是不是get请求中的query请求?
在这里插入图片描述
第一种 params方式

// params 对象内写形式
getInstructorById1(id) {
      axios({
        method: "get",
        url: "http://localhost:8080/instructor/getInstructorById",
        params: {
          id: id,
          //如果 key value名字相同可简写 例如 id:id可以写为 id
        },
      }).then(function (res) {
        console.log(res);
      });
    },
//params 对象外写形式
    getInstructorById3(id) {
      const params = {
        id: id,
        //如果 key value名字相同可简写 例如 id:id可以写为 id
      };
      axios({
        method: "get",
        url: "http://localhost:8080/instructor/getInstructorById",
        params,
      }).then(function (res) {
        console.log(res);
      });
    },

第二种 拼接方式

 //?a="a"&b="b" 拼接形式
 getInstructorById2(id) {
      axios({
        method: "get",
        url: `http://localhost:8080/instructor/getInstructorById?id=${id}`,
        //与下面两种写法都可以
        // url: "http://localhost:8080/instructor/getInstructorById?id=" + id,
      }).then(function (res) {
        console.log(res);
      });
    },

path

如何区分是Get中的path形式
在这里插入图片描述

//path
    getById1(id) {
      axios({
        method: "get",
        // url: "http://localhost:8080/instructor/getById/" + id,
        //两种写法都可以
        url: `http://localhost:8080/instructor/getById/${id}`,
      }).then(function (res) {
        console.log(res);
      });
    },

post请求

query

如何区分是post query请求
在这里插入图片描述
第一种 params

//post params
    add1(id) {
      axios({
        method: "post",
        url: "http://localhost:8080//instructor/add",
        params: {
          id: id,
          name: "jhj",
        },
      }).then((res) => {
        console.log(res);
      });
    },

第二种 formdata

 add2(id) {
      const params = {
        id,
        name: "jhj",
      };
      axios({
        method: "post",
        url: "http://localhost:8080//instructor/add",
        data:params,
        headers:{
          'content-type':"multipart/form-data"
        }
      }).then((res) => {
        console.log(res);
      });
    },
add3(id) {
      const data=new FormData();
      data.append("name","jhj")
      data.append("id",id)
      axios({
        method: "post",
        url: "http://localhost:8080//instructor/add",
        data:data,
      }).then((res) => {
        console.log(res);
      });
    },

body

如何区分 是body post请求
在这里插入图片描述

addbody(id) {
      axios({
        method: "post",
        url: `http://localhost:8080/instructor/addbody`,
        data: {
          id: id,
          name: "jhj",
        },
        //请求中没有 data参数 所以headers content-type 默认为 content-type: "application/json;charset=UTF-8"
        // headers: {
        //   // 'content-type': 'application/json;charset=UTF-8' 默认
        //   // "Content-Type": "application/x-www-form-urlencoded",
        //   // "content-type":"multipart/form-data"
        // },
      }).then(function (res) {
        console.log(res);
      });
    },

header

在这里插入图片描述

putheader(id) {
      axios({
        method: "post",
        url: `http://localhost:8080/instructor/putheader`,

        //自定义header
        headers: {
          // 'content-type': 'application/json;charset=UTF-8'
          // "Content-Type": "application/x-www-form-urlencoded",
          // "content-type":"multipart/form-data"
          myheader: id,
        },
      }).then(function (res) {
        console.log(res);
      });
    },

delete

在这里插入图片描述

 //deleted params
    del1(id) {
      axios({
        method: "delete",
        url: "http://localhost:8080/instructor/delete",
        params: {
          id: id,
          //如果 key value名字相同可简写 例如 id:id可以写为 id
        },
      }).then(function (res) {
        console.log(res);
      });
    },

put

put query

在这里插入图片描述

put(id) {
      axios({
        method: "put",
        url: `http://localhost:8080/instructor/put`,
        params: {
          id: id,
          name: "jhj",
        },
      }).then(function (res) {
        console.log(res);
      });
    },

put body

在这里插入图片描述

 putbody(id) {
      axios({
        method: "put",
        url: `http://localhost:8080/instructor/putbody`,
        data: {
          id: id,
          name: "jhj",
        },
      }).then(function (res) {
        console.log(res);
      });
    },

综合

在这里插入图片描述

all(id) {
      axios({
        method: "post",
        url: `http://localhost:8080/instructor/all/${id}`,
        params: {
          ids:id
        },
        data: {
          id,
          name: "jhj",
        },
        //自定义header
        headers: {
          // 'content-type': 'application/json;charset=UTF-8'
          // "Content-Type": "application/x-www-form-urlencoded",
          // "content-type":"multipart/form-data"
          myheader: id,
        },
      }).then(function (res) {
        console.log(res);
      });
    },

作者声明

如有问题,欢迎指正!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

axios请求 的相关文章

随机推荐

  • Mysql问题Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column

    java sql SQLSyntaxErrorException Expression 2 of SELECT list is not in GROUP BY clause and contains nonaggregated column
  • 【SAP-FI】承诺项目(Commitment item)详解

    定义 xff1a 承诺项目表示组织在财务管理区域 xff08 FM区域 xff09 内的功能分组 用途 xff1a 承诺项目将影响流动性的预算交易和商业交易分类为收入 xff0c 支出和现金余额项目 您可以将特定责任区域 xff08 资金中
  • 操作系统--03内存管理

    内存管理 第三章 xff1a 内存管理 xff08 存储器管理 xff09 3 内存保护的两种办法 xff1a 3 1 覆盖与交换3 2 连续分配管理方式3 3 动态分区分配算法1 首次适应算法 xff1a 2 最佳适应算法 xff1a 3
  • SCRUM敏捷项目管理实战(深圳站)

    1 内容提要 SCRUM是目前各互联网公司普遍采用的敏捷项目管理模式 xff0c 与传统的项目管理十大知识领域相比 xff0c 敏捷更加直击要害 xff0c 更加强调自组织和跨职能团队 xff0c 更能帮助企业高效率交付和盈利 xff01
  • 2021年最新gitee使用教程

    gitee简介 Gitee com xff08 码云 xff09 是 OSCHINA NET 推出的代码托管平台 xff0c 支持 Git 和 SVN xff0c 提供免费的私有仓库托管 目前已有超过 600 万的开发者选择 Gitee 为
  • 在vscode中运行c、c++(超级简单)

    第一 下载安装vscode 第二 下载插件 链接 xff1a https pan baidu com s 1mLdKbQWxkZJYhwH0ToD9oQ 提取码 xff1a 3kxe 复制这段内容后打开百度网盘手机App xff0c 操作更
  • flameshot安装并配置插入文字描述、设置默认保存路径、将截图内容添加到粘贴板中

    flameshot配置插入文字描述 设置默认保存路径 将截图内容添加到粘贴板中 安装 xff1a https github com flameshot org flameshot releases 下载相应rpm包 xff0c 安装即可 以
  • 静态域[详解]

    不知道静态域是什么 目前有两种想法 1是代表static修饰的属性 方法等的集合 即所有static修饰的都算 2是认为仅仅代表静态代码块 即 static 下面正式研究 34 何为静态域 34 查到的文章基本分静态域 静态常量 静态方法这
  • OpenFlow 流表

    流规则组成 xff1a 每条流规则由一系列字段组成 xff0c 分为基本字段 条件字段和动作字段三部分 一 xff1a 基本字段 duration sec xff1a 表示流表项的生效时间 xff0c 以秒为单位 可以用来控制流表项的生命周
  • Gittee的使用

    Git Linus用C写的分布式版本控制系统 Git官网 xff1a https git scm com Gittee 国内代码托管和协作开发的平台 xff0c 可以看作为中文版的 GitHub 官网 xff1a Gitee 基于 Git
  • 使用VsCode管理Gitee仓库中的项目

    使用VsCode管理Gitee仓库中的项目的大致流程如下 1 首先得下载安装 git xff0c 详见 Git 详细安装教程详解 Git 安装过程的每一个步骤 mukes的博文 xff09 2 为 git 配置 username和email
  • Linux嵌入式开发之内存占用

    一 引言 内存是嵌入式系统中的关键资源 xff0c 内存占用主要是指软件系统的内存使用情况 本篇博客将介绍如何分析内存使用以便进行进一步优化内存占用相关的基础概念和相关工具 二 内存占用 内存占用是应用程序运行时内存的使用或引用数量 对于开
  • 手眼标定——使用 easy_handeye 和 aruco

    整个过程分为以下三步 aruco ros 的配置使用easy handeye 的配置使用标定过程 aruco 的配置使用 clone aruco 项目 到 ros 工作空间 前往 aruco marker 生成网站 打印 marker xf
  • CentOS7.6 Docker 操作(一)

    CentOS7 6 Docker 操作 xff08 一 xff09 CentOS 7 6镜像地址 网易镜像 xff08 可直接复制地址到迅雷 xff0c 下载会快一些 xff09 http mirrors 163 com centos 7
  • 读取excel 表格控件

    直接在实时编辑器里 xff1a T 61 xlsread 39 C Users 86173 Desktop DESKETOP 111 xlsx 39 t 61 textread 39 C Users 86173 Desktop DESKET
  • Eureka注册中心

    3 Eureka注册中心 假如我们的服务提供者user service部署了多个实例 xff0c 如图 xff1a 大家思考几个问题 xff1a order service在发起远程调用的时候 xff0c 该如何得知user service
  • 从docker 拉去指定版本的镜像

    从docker 拉去指定版本的镜像 1 上https hub docker com 网站 xff0c 查询 点击tags查看 2 拉取 docker pull images tags
  • SpringBoot整合mybatis-plus

    导入依赖 在项目pom文件导入依赖 在项目pom文件导入依赖 span class token tag span class token tag span class token punctuation lt span dependency
  • idea mybatisplus 插件使用

    在plugin中安装mybatisplus 插件 使用 配置数据库 生成代码 表新增字段 xff0c 重新生成实体类覆盖 因业务需求 xff0c 表中可能会时不时增加一些字段 xff0c 大多情况下实体类中不会添加表中没有的字段 xff0c
  • axios请求

    可传参数 span class token function axios span span class token punctuation span span class token punctuation span span class