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请求 的相关文章

  • 如何在 React Query 中将 debounce 与 useQuery 一起使用?

    我正在使用 React Query 从 React 应用程序中的 API 获取数据 我想实现去抖以获得更好的性能 但我无法让它与 useQuery 一起使用 当我尝试将 API 调用包装在去抖函数中时 收到一条错误消息 查询函数必须返回定义
  • axios 在本机反应中给出 [AxiosError: Network Error]

    我是 React Native 的新手 我正在尝试使用 React Native 中的 axios 提交 api 但我 getiign AxiosError 网络错误 我不知道这是什么或如何解决这个问题 function getdata c
  • Nuxt.js - API 调用的最佳场所

    我是 Vue js Nuxt 和所有前端东西的新手 我有一个关于 API 调用的问题 我不确定什么是正确的方法 这里的最佳实践是什么 我有一家商店 在该商店中 我有调用我的 API 并设置状态的操作 例如 async fetchArticl
  • 多个 API 调用时仅运行一次响应拦截器

    我有一个这样的拦截器 axios interceptors response use undefined err gt const error err response console log error if error status 4
  • 将以下 Paypal curl 命令转换为 axios?

    如何将此 paypal curl 命令转换为 Axios curl v https api sandbox paypal com v1 oauth2 token H Accept application json H Accept Lang
  • 我通过 Axios 来自 API 并使用 Vue 显示的数据未显示

    我刚刚开始学习 Vue Js 我试图从 API 中获取一些信息并将其显示在表格中 我真的看不出问题出在哪里 我彻底浏览了代码 但无法弄清楚问题出在哪里 下面是我写的代码 我很漂亮 这是一件小事 或者在当前版本的 Vue 中是否有新的方法来做
  • Node 中express.js 和 axios.js 的区别

    我们使用axios来进行get post等http请求 我们也出于同样的目的使用快递 然而 根据我读到的内容 它们有不同的目的 请解释一下如何 PS 如果能举例说明就太好了 你可以将express js视为一个仓库 app get item
  • Axios 通过 Django REST Framework 被 CORS 策略阻止

    我正在尝试使用 Axios 向我的 API Django REST Framework 发出请求 但出现以下错误 Access to XMLHttpRequest at http trvl hopto org 8000 api airpor
  • 类型错误:func.apply 不是函数

    我正在尝试使用 useEffect 函数 如下所示 const data setData useState courses useEffect async gt const result await axios get http examp
  • 如何从 MongoDB 获取数据?

    我正在尝试使用 Express MongoDB 构建 React 应用程序 我能够将一些文档发布到 MongoDB 目前 我正在尝试弄清楚如何将获取的数据打印到屏幕上 我有这些路线 router post totalbalance requ
  • 使用 axios 递归获取数据并链接结果

    我有一个模式的网址http www data com 1 其中末尾的 1 可以一直运行到一个未预定义的数字 它返回一个数组 我需要将我得到的所有数组连接成一个 我的策略是递归执行 get 请求 直到收到 404 错误 然后返回结果 var
  • axios catch 没有捕获错误

    我在用着axios 0 17 0使用以下代码 this props userSignupRequest this state then res gt console log res data catch err gt this setSta
  • Fetch / Axios 在 React Native 中严重崩溃(但仅限于某些 URL)

    我的应用程序在执行时严重崩溃certainAPI 调用 我将范围缩小到这一点 这不是 HTTP 与 HTTPS 的问题 我最终使用了两种不同的模拟 API 令我惊讶的是 一种有效 另一种则无效 两者基本相同 请参阅下面的片段 WIFI 或蜂
  • 在react js中调用axios get请求时出现网络错误

    我在 macOS 中使用 React js 当我尝试调用时axios get 我收到网络错误 我读过许多像我一样使用 React Native 的其他案例 答案是添加设置以允许他们使用http在 Mac 中而不是https 但是该设置不能在
  • 如何防止 Axios 对我的请求参数进行编码?

    我正在尝试通过 GET 请求中的 URL 参数传入 API 密钥 但是 我注意到 Axios 在发送请求时对我的 API 密钥中的字符进行编码 这会导致 API 拒绝我的请求 因为它无法识别我的密钥 如何防止 Axios 对我的 GET 参
  • 如何用 jest 测试 axios 拦截器

    在我的项目中 我有一个命名空间 导出一些使用 Axios 的函数 在同一个文件中 我向 axios 实例添加一个拦截器 如下所示 axios interceptors response use res gt res error gt if
  • 将 axios POST 请求与 moxios 匹配

    是否可以使用 moxios 模拟对 POST 请求的回复 不仅通过 URL 匹配 还通过 POST 正文匹配 事后检查尸体对我来说也很有用 这就是我现在正在做的事情 据我所知 没有特定于方法的存根方法 describe createCode
  • Axios POST 请求不使用“multipart/form-data”发送任何内容 [React Native - Expo]

    Scenario 前端基本上是一个 React Native Expo 应用程序 用户可以在其中发布报告 这包括拍摄多张照片并填写一些详细信息 后端只是node js 带有 Express 和 Multer Problem 我使用 Axio
  • 在ReactJS中导出axios实例后如何修改它?

    我在用 axios defaults headers common Authorization Bearer token 在用户登录应用程序后设置标题 但刷新页面时此配置将被删除 当用户登录时 我想为来自 axios 的所有请求设置此配置
  • 即使使用 withCredentials:true,Axios 也不发送 cookie 数据

    尝试使用 React 和 Express 发出请求并发送 cookie 请求 响应工作正常 但 cookie 并未发送 在客户端 import axios from axios let endPoint http 192 168 1 135

随机推荐

  • 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