axios 各种方式的请求 示例

GET请求

示例一:

  • 服务端代码
@GetMapping("/f11")
public String f11(Integer pageNum, Integer pageSize) {return pageNum + " : " + pageSize;
}
  • 前端代码
<template><div class="home"><button @click="getFun1">发送get请求</button></div>
</template>
<script>import axios from 'axios'export default {name: 'Home',methods: {getFun1 () {axios.get('http://localhost/blog/f11?pageNum=12&pageSize=8').then(res => {console.log(res)})}}}
</script>

示例二:

  • 服务端代码
@GetMapping("/f12")
public String f12(Integer pageNum, Integer pageSize, HttpServletRequest request) {String token = request.getHeader("token");return pageNum + " : " + pageSize + " : " + token;
}
  • 前端代码
<template><div class="home"><button @click="getFun2">发送get请求</button></div>
</template>
<script>import axios from 'axios'export default {name: 'Home',methods: {getFun2 () {axios.get('http://localhost/blog/f12', {params: {pageNum: 12,pageSize: 8},headers: {token: 'asdf123456'}}).then(res => {console.log(res)})}}}
</script>

GET方式采用接口方式携带参数,比如上面示例最终请求服务器端的url是:
在这里插入图片描述

POST请求

示例一:

  • 服务端代码
@PostMapping("/f21")
public String f21(@RequestBody String param) {return param;
}
  • 前端代码
<template><div class="home"><button @click="postFun1">发送post请求</button></div>
</template>
<script>import axios from 'axios'export default {name: 'Home',data () {return {queryInfo1: {query: {username: 'zhangsan',password: '1234'}}}},methods: {postFun1 () {let _this = thisaxios.post('http://localhost/blog/f21', _this.queryInfo1).then(res => {console.log(res)})},}}
</script>

结果:
在这里插入图片描述
在这里插入图片描述

示例二:

  • 服务端代码
@PostMapping("/f21")
public String f21(@RequestBody String param) {return param;
}
  • 前端代码
<template><div class="home"><button @click="postFun2">发送post请求</button></div>
</template>
<script>import axios from 'axios'export default {name: 'Home',data () {return {queryInfo2: {username: 'zhangsan',password: '1234'}}},methods: {postFun2 () {let _this = thisaxios.post('http://localhost/blog/f21', {data: _this.queryInfo2}).then(res => {console.log(res)})}}}
</script>

结果:
在这里插入图片描述
在这里插入图片描述

示例三:

  • 服务端代码
@PostMapping("/f23")
public String f23(Integer aa, Integer bb,@RequestBody String query) {return aa + ": " + bb + ": " + query;
}
  • 前端代码
<template><div class="home"><button @click="postFun3">发送post请求</button></div>
</template>
<script>import axios from 'axios'export default {name: 'Home',data () {return {queryInfo2: {username: 'zhangsan',password: '1234'}}},methods: {postFun3 () {let _this = thisaxios.post('http://localhost/blog/f23', _this.queryInfo2, {params: { //params表示url中传递的参数,它会拼接在url后面aa: 11,bb: 22}}).then(res => {console.log(res)})}}}
</script>

请求的url为:http://localhost/blog/f23?aa=11&bb=22 ,结果:
在这里插入图片描述
在这里插入图片描述

注意上面三个示例中传递到后台的username和password参数采用下面方式后台是无法获取到的:

@PostMapping("/f22")
public String f22(String username, String password) {return username + " : " + password;
}

原因是axios.post默认情况下传递到后台的数据是JSON格式的,通过设置POST请求头,可以告诉服务器请求主体的数据格式为kv的形式,比如:a=aaaa&b=bbbb。

示例:设置POST请求的格式

  • 后台代码
@PostMapping("/f21")
public String f21(@RequestBody String param) {return param;
}
  • 前端代码
<template><div class="home"><button @click="postFun1">发送post请求</button><button @click="postFun2">发送post请求</button></div>
</template>
<script>import axios from 'axios'axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'import qs from 'qs'export default {name: 'Home',methods: {postFun1 () {let params = new URLSearchParams()params.append('username', 'zhangsan')params.append('password', '1234')axios.post('http://localhost/blog/f22', params).then(res => {console.log(res)})},postFun2 () {let params = qs.stringify({'username': 'zhangsan','password': 1234})axios.post('http://localhost/blog/f22', params).then(res => {console.log(res)})},}}
</script>

前端会将参数以kv字符串的形式发送到后台:username=zhangsan&password=1234。上面示例前端网页中请求的也可以用下面控制器接收:

@PostMapping("/f22")
public String f22(String username, String password) {return username + " : " + password;
}

Put

示例一:

  • 前端
let _this = this
_this.$axios.put(`/user/${user.id}/status`).then(res => {  //注意,此处使用的是反斜杠console.log(res)
})
  • 后端
@PutMapping("/user/{userId}/status")
public Result changStatus(@PathVariable("userId") Integer userId){}

示例二:

  • 前端
const param = {userId:1
}
_this.$axios.put('/user/update',param).then(res=>{console.log(res)
})
  • 后端
@PutMapping("/user/update")
public Result changStatus(@PathVariable("userId") Integer userId){}

patch

前端

const param={ids:[1,3,5,8]
}
_this.$axios.patch('/user/p',param).then(res=>{console.log(res)
}),

Delete

前端

_this.$axios.delete('/user/delete',{params:{id:1}}).then(res=>{console.log(res)})

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/47484.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

DP读书:鲲鹏处理器 架构与编程(六)PCI Express 总线

处理器与服务器&#xff1a;PCI Express 总线 PCI Express 总线1. PCI Express 总线的特点a. 高速差分传输b. 串行传输c. 全双工端到端连接d. 基于多通道的数据传输方式e. 基于数据包的传输 2. PCI Express 总线的组成与拓扑结构a. 根复合体b. PCI Express桥c. 功能单元 3. PCI…

【正点原子STM32连载】第十九章 通用定时器输入捕获实验 摘自【正点原子】APM32F407最小系统板使用指南

1&#xff09;实验平台&#xff1a;正点原子stm32f103战舰开发板V4 2&#xff09;平台购买地址&#xff1a;https://detail.tmall.com/item.htm?id609294757420 3&#xff09;全套实验源码手册视频下载地址&#xff1a; http://www.openedv.com/thread-340252-1-1.html# 第十…

hive lag() 和lead()函数

LAG 和 LEAD函数简介 Hive 中的 LAG 和 LEAD 函数时&#xff0c;通常用于在结果集中获取同一列在前一行&#xff08;LAG&#xff09;或后一行&#xff08;LEAD&#xff09;的值。这在分析时间序列数据、计算变化率或查找趋势时非常有用。以下是这两个函数的用法示例&#xff1…

2023七夕小程序

又是一年七夕节 往年七夕小程序 2020 https://blog.csdn.net/chen_227/article/details/107062998 2022 视频 QiXi2022 代码 https://gitee.com/chen227/qixi2022-qt-qml 2023 效果 代码 https://gitee.com/chen227/qixi2023-qt-qml

Android Studio中引入MagicIndicator

1.github中下载文件 GitHub - hackware1993/MagicIndicator: A powerful, customizable and extensible ViewPager indicator framework. As the best alternative of ViewPagerIndicator, TabLayout and PagerSlidingTabStrip —— 强大、可定制、易扩展的 ViewPager 指示器框…

【大模型AIGC系列课程 1-2】创建并部署自己的ChatGPT机器人

OpenAI API 调用 获取 openai api api-key https://platform.openai.com/account/api-keys 利用 python requests 请求 openai 参考 openai 接口说明:https://platform.openai.com/docs/api-reference/chat/create import json # 导入json包 import requests # 导入req…

TCP特点UDP编程

目录 1、tcp协议和udp协议 2、多线程并发和多进程并发&#xff1a; &#xff08;1&#xff09;多进程并发服务端 &#xff08;2&#xff09;多进程并发客户端&#xff1a; 3、tcp: 4、粘包 5、UDP协议编程流程 (1)服务器端&#xff1a; (2)客户端&#xff1a; 6、tcp状…

Java请求Http接口-hutool的HttpUtil(超详细-附带工具类)

概述 HttpUtil是应对简单场景下Http请求的工具类封装&#xff0c;此工具封装了HttpRequest对象常用操作&#xff0c;可以保证在一个方法之内完成Http请求。 此模块基于JDK的HttpUrlConnection封装完成&#xff0c;完整支持https、代理和文件上传。 导包 <dependency>&…

大数据向量检索的细节问题

背景:现有亿级别数据(条数),其文本大小约为150G,label为字符串,content为文本。用于向量检索,采用上次的试验进行,但有如下问题需要面对: 1、向量维度及所需空间 向量维度一版采用768的bert系列的模型推理得到,openai也有类似的功能,不过是2倍的维度(即1536),至…

gin中关于参数注入问题

关于参数注入的问题 如果在开发中一旦发小参数没有按照既定的要求注入到结构体的话&#xff0c;这个时候就一定要看请求方式什么&#xff1f;如果是post请求、 前端—post—json{id:1,pageSize:10,page:1}———————————- 参数注入方法&#xff1a;ShouldBindJSON p…

Android事件分发机制被我翻烂了

作者&#xff1a;积木zz 这次说下Android中的事件分发机制 从开始点击屏幕开始&#xff0c;就会产生从Activity开始到decorview一直到最里层的view一连串事件传递。每一层view或者viewgroup都会首先调用它的dispatchTouchEvent方法&#xff0c;然后判断是否就在当前一层消费掉事…

LLaMA模型泄露 Meta成最大受益者

一份被意外泄露的谷歌内部文件&#xff0c;将Meta的LLaMA大模型“非故意开源”事件再次推到大众面前。“泄密文件”的作者据悉是谷歌内部的一位研究员&#xff0c;他大胆指出&#xff0c;开源力量正在填平OpenAI与谷歌等大模型巨头们数年来筑起的护城河&#xff0c;而最大的受益…

selenium中处理验证码问题

验证码 基本作用&#xff1a;可以实现当前访问页面的数据安全性、还可以减少用户的并发数&#xff1b; 类型&#xff1a;1、纯数字、纯字母&#xff1b;2、汉字组合&#xff1b;3、数学运算题&#xff1b;4、滑动&#xff1b;5、图片&#xff08;选不同的、选相同、成语顺序&…

Ubuntu 20.04使用Livox mid 360 测试 FAST_LIO

前言 Livox mid360需要使用Livox-SDK2&#xff0c;而非Livox-SDK&#xff0c;以及对应的livox_ros_driver2 。并需要修改FAST_LIO中部分代码。 1. 安装Livox-SDK2 参考官方教程。 1.1. 安装CMake sudo apt install cmake1.2. 安装编译Livox-SDK2 git clone https://github…

聚观早报 | 网龙发布EDA白皮书;日产合资旗下品牌使用东风纯电

【聚观365】8月22日消息 网龙发布EDA白皮书 日产合资公司旗下自主品牌将使用东风纯电平台 vivo Pad Air评测 辛巴818五周年专场带货GMV达22.3亿 X删除2014年12月前大多数图片和推文链接 网龙发布EDA白皮书 近日消息&#xff0c;由北京师范大学和联合国教科文组织教育信息…

ThreadLocal深度解析

简介 在并发编程中&#xff0c;导致并发bug的问题都会归结于对共享变量的操作不当。多个线程同时读写同一共享变量存在并发问题&#xff0c;我们可以利用写时复制、不变性来突破对原数据的写操作&#xff0c;没有写就没有并发问题&#xff0c;而本篇文章所介绍的技术是突破共享…

centos7.9升级openssl以解决pip安装过程中的问题

查看原来openssl的版本 openssl version -a | OpenSSL 1.0.2k-fips 26 Jan 2017 built on: reproducible build, date unspecified platform: linux-x86_64 options: bn(64,64) md2(int) rc4(16x,int) des(idx,cisc,16,int) idea(int) blowfish(idx) compiler: gcc -I. -I.…

完美版积分商城系统-奇偶商城系统源码+独立代理后台

奇偶商城系统源码 完美版独立代理后台 1.演示环境&#xff1a;Linux Centos7以上版本 宝塔 2.Nginx 1.18.0 PHP7.0 Mysql5.6 3.伪静态选择thinkphp 4./Application/Common/Conf 修改数据库信息 详细搭建教程附在压缩包内了,下载查看

Vue3.X 路由与导航栏、侧边栏(四)

我们接着上一节的 Vue3.x 生命周期&#xff08;三&#xff09; 的说明&#xff0c;我们这一节讲解了项目中路由的配置与导航栏、侧边栏的关系。 一、路由配置 vue项目中路由配置有一个固有文件夹&#xff0c;可以配置路由&#xff0c;这样的优点使项目更加清晰明了。 如图&a…

美创科技荣获“2023年网络安全优秀创新成果大赛—杭州分站赛”两项优胜奖

近日&#xff0c;由浙江省互联网信息办公室指导、中国网络安全产业联盟&#xff08;CCIA&#xff09;主办&#xff0c;浙江省网络空间安全协会承办的“2023年网络安全优秀创新成果大赛-杭州分站赛”正式公布评选结果。 经专家评审&#xff0c;美创科技报名参赛的解决方案—“医…