位置: IT常识 - 正文

axios—使用axios请求REST接口—发送get、post、put、delete请求(axios怎么使用)

编辑:rootadmin
axios—使用axios请求REST接口—发送get、post、put、delete请求

推荐整理分享axios—使用axios请求REST接口—发送get、post、put、delete请求(axios怎么使用),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:axios有哪些常用方法,axios有哪些常用方法,axios的使用,axios的基本使用,axios.all 使用,axios有哪些常用方法,axios django,axios.all 使用,内容如对您有帮助,希望把文章链接给更多的朋友!

文档:GitHub - axios/axios: Promise based HTTP client for the browser and node.js

目录

一、axios发送get请求

简写版get请求

完整版get请求

get请求怎么在路径上携带参数

二、axios发送post请求

简写版post请求

完整版post请求

其他方式发送post请求

三、axios发送put请求

简写版 put请求

完整版put请求

四、axios发送delete请求删除操作

简写版delete请求

完整版delete请求

其他delete请求的方式:


一、axios发送get请求

我们使用get请求可以得到我们想要的具体的数据

then方法指定成功时候的回调

<button onclick="testGet()">Get请求</button>简写版get请求 // 发送get请求 function testGet(){ // 这个参数是添加请求地址 axios.get('http://localhost:3000/posts') // 使用.then获取数据 .then(response=>{ console.log('/posts get请求',response.data) }) }

完整版get请求 // 指定默认配置 axios.defaults.baseURL='http://localhost:3000' // axios.get('http://localhost:3000/posts?id=1') 之前的写法 // 传入配置对象的方式 axios({ // url:'http://localhost:3000/posts', // 因为我们配置了 axios.defaults.baseURL='http://localhost:3000' 这里可以用简写方式 url:'/posts', // 请求参数 params:{ id:1 } }) .then(response=>{ console.log('/posts get请求',response.data) })get请求怎么在路径上携带参数axios—使用axios请求REST接口—发送get、post、put、delete请求(axios怎么使用)

直接拼接到url上面就可以了

@GetMapping("/getQueryPage/{currentPage}/{pageSize}") public ResultObject getQueryPage(@PathVariable("currentPage") int currentPage, @PathVariable("pageSize") int pageSize, Book book){ IPage<Book> ipage= bookService.getQueryPage(currentPage,pageSize,book);// ResultObject r = new ResultObject(true,ipage); return new ResultObject(true,ipage); } // 请求服务器获取所有书籍信息 getAllBooks(){ // 第一个books是代理服务器中我们配置的 this.$http.get('/books/books/getQueryPage/'+this.pageNum+'/'+this.pageSize,this.queryBooks) .then( response=>{ // console.log("get books",response.data.data) // this.booksList=response.data.data // console.log(this.booksList) console.log("get books",response.data.data) }, error=>{ this.$message.error('服务器错误,获取书籍列表失败') } ) },

 

其他方式发送get请求 data(){ return{ // 查询条件 queryInfo:{ type:3, pagenum:1, pagesize:5 }, // 商品分类的数据列表,默认为空 catelist:[] } }, created(){ this.getCateList() }, methods:{ // 获取商品分类数据 getCateList(){ this.$http.get('categories',{params:this.queryInfo}) } }二、axios发送post请求

我们可以操作post请求增加一条或者多条数据,可以采用JSON的形式传输数据

<button onclick="testPost()">Post请求</button>简写版post请求// 发送post请求 function testPost(){ // post请求的数据以对象的形式传输 axios.post('http://localhost:3000/posts', { "title": "json-server3", "author": "typicode3" }) .then(response=>{ console.log('/posts post请求',response.data) }) }

完整版post请求// 指定默认配置axios.defaults.baseURL='http://localhost:3000' axios({ url:'/posts', method:'post', // json字符串的格式 data:{ "title": "json-server3", "author": "typicode3" } }) .then(response=>{ console.log('/posts post请求',response.data) })其他方式发送post请求 // {rids:idStr} 服务器需要传输一个rids的请求体 this.$http.post(`roles/${this.roleId}/rigths`,{rids:idStr}) // 真正的还书方法 returnBook(){ // 表单先预验证一下 this.$refs.returnBooksRef.validate(valid=>{ if(!valid){ return this.$message.error("密码不可为空,无法还书!") } // 验证密码 if( !(this.userPassword===this.returnBooksList.password)){ return this.$message.error("密码错误,无法还书!") } // console.log("returnBooksList 发送请求前",this.returnBooksList) 没问题 // 运行到这个地方说明密码正确 //最重要的一步:发请求还书 this.$http.post("/borrowBookProxy/borrowBooks/returnBooks",this.returnBooksList) .then( response=>{ // console.log("returnBooksList",this.returnBooksList) 没问题 console.log("return return",response.data) if(!response.data.data){ return this.$message.error("还书失败!请您刷新列表检查是否需要归还此书!") } this.$message.success("归还书籍成功!") this.returnBooksList.password='' this.getUser() this.returnBookDialogVisible=false }, error=>{ this.$message.error("服务器出现错误,稍后重试") } ) }) },三、axios发送put请求

我们采put请求修改数据,可以具体修改某一条数据

<button onclick="testPut()">PUT请求</button> 简写版 put请求 // 发送put请求修改 function testPut(){ // 修改id=3的内容,修改为后面的参数 axios.put('http://localhost:3000/posts/3', { "title": "json-server....", "author": "typicode3..." }) .then(response=>{ console.log('/posts put请求',response.data) }) }

完整版put请求 axios({ // url:'/posts', // params:{ // id:1 // }, url:'/posts/1', //这个是上面的简写形式 method:'put', data: { "title": "json-server....", "author": "typicode3..." } }).then(response=>{ console.log('/posts put请求',response.data) })四、axios发送delete请求删除操作 <button onclick="testDelete()">DELETE请求</button>简写版delete请求 // 发送delete请求修改 function testDelete(){ // 删除id=3的数据 axios.delete('http://localhost:3000/posts/3') .then(response=>{ console.log('/posts delete请求',response.data) }) }

完整版delete请求 axios({ url:'/posts/3', method:'delete' }).then(response=>{ console.log('/posts delete请求',response.data) })其他delete请求的方式:removeRightById(role,rightId){ this.$http.delete(`roles/${role.id}/rights/${rightId}`)}
本文链接地址:https://www.jiuchutong.com/zhishi/284151.html 转载请保留说明!

上一篇:最小的索尼运行相机是什么(索尼体积最小的微单)

下一篇:ant-design-vue 自由切换 暗黑模式dark(ant-design-vue pro)

  • 营业税加收滞纳金的规定
  • 跨年增值税发票怎么冲红
  • 税收职能和作用
  • 以前年度亏损弥补会计分录
  • 企业取得非货币性收入
  • 用银行存款上交各种税费
  • 购入汽车
  • 通讯费可以抵扣进项税吗
  • 出口货物退税率查询
  • 行政诉讼适用范围
  • 失控进项发票转出 补税做账
  • 小型微利企业所得税优惠政策
  • 增值税普通发票需要交税吗
  • 想要房贷利息抵税怎么办
  • 公司成立后一直没有收入
  • 公司减少注册资本的程序
  • 调拨资产入账按原值还是按净值
  • 领用原材料安装固定资产
  • 灭火器属于办公设施吗
  • 软件进项税额分摊方式
  • windows10无法打开图片
  • uniapp分享图片
  • 一般纳税人开出普票可以抵扣吗
  • 违建罚款了还算违建吗
  • 快捷方式都变成pdf
  • 你知道怎么安装
  • 小规模纳税人销售自己使用过的汽车
  • php的使用
  • php可以连接access吗
  • 业务招待费税务筹划案例
  • 增值税税率为零的有哪些项目
  • 劳动法律法规包含哪些
  • 接口500错误原因
  • 帝国cms移动端
  • 同业代付融资
  • 增值税有哪些类型的税种
  • 斐讯路由器怎么重置
  • call for conference paper
  • 外商投资企业和外资企业的区别
  • 梵净山原名
  • vue数据更新会触发什么生命周期
  • 销售赔偿款增值税和所得税
  • vue项目安装路由
  • 如何做架构规划图
  • ussd命令
  • 折扣店商场
  • 销售价格低于成本价,税务机关是否有权纳税调整?
  • wordpress怎么降级
  • 土地给人家种了几十年还能要回来吗
  • 瀑布流样式
  • mongo mysql区别
  • python 概率分布函数
  • 分公司产生的费用怎么报销
  • 房产税的定义是什么
  • 跨月开票的业务怎么做
  • 产品成本归集和分配是哪一章的
  • 残保金中上年在职职工工资总额
  • 单位发放职工2000元慰问金申请怎么写
  • 对外投资未实缴怎么办
  • 金税盘离线开票时间超限的处理方法
  • 代开的专票作废了怎么做账?
  • 企业预付账款的分录
  • sqlserver将查询结果作为临时表
  • mysql备份与恢复数据库
  • mysql数据库中储存的是
  • win2003系统要求
  • mssearch.exe - mssearch是什么进程 有什么用
  • win8系统怎么调出运行窗口
  • win10天气系统怎么弄掉
  • Node.js中的全局变量有哪些
  • jquery控制display属性
  • python中的__dict__
  • nodejs调用打印机
  • 老生常谈的近义词
  • 完美世界打斗
  • 安卓多线程有几种实现方法
  • 如何让listview提高效率
  • 小规模国税申报表填写方法
  • 乡镇地税局待遇
  • 广东省国家税务总局班子成员
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设