当前位置: 首页 > news >正文

Javaweb基础-axios

Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中。

GET方法

get请求第一种写法

//后端
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {@RequestMapping("/getTest")// 被@RequestParam标记的参数必传,并且参数名前后端一致public Map<String, Object> getTest(@RequestParam String param) {log.info("入参:{}", param);HashMap<String, Object> map = new HashMap<>(2);map.put("param", param);log.info("出参:{}", map);return map;}
}
//axios
export default {name: 'App',methods: {getTest: function () {this.$axios({method: "GET",        //方法url: "/demo/getTest", //访问路径params: {             //参数 把参数拼接到url后面=》/demo/getTest?param=hiparam: "hi"        // 后端被@RequestParam标记的参数名为param}}).then(res => {        //返回参数console.log(res)}).catch(err => {       //异常alert(err)})}}
}

get请求第二种写法

//后端
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {@RequestMapping("/getTest")// 被@RequestParam标记的参数必传,并且参数名前后端一致public Map<String, Object> getTest(@RequestParam String param) {log.info("入参:{}", param);HashMap<String, Object> map = new HashMap<>(2);map.put("param", param);log.info("出参:{}", map);return map;}
}
//axios
export default {name: 'App',methods: {getTest2: function () {this.$axios.get("/demo/getTest?param=hello")    //方法名("访问路径?参数名=参数值"),.then(res => {                       //返回数据console.log(res)}).catch(err => {                      //异常alert(err)})},}
}

get请求发送的请求不能使用被@RequestBody标记的对象接收。

POST方法

post请求第一种写法

    //后端@RequestMapping(value = "/postTest", method = RequestMethod.POST)//被@RequestParam标记的参数必传,并且参数名前后端一致public Map<String, Object> postTest(@RequestParam String param) {log.info("入参:{}", param);HashMap<String, Object> map = new HashMap<>(2);map.put("param", param);log.info("出参:{}", map);return map;}//axios
export default {name: 'App',methods: {postTest: function () {this.$axios({method: "POST",            //方法url: "/demo/postTest",     //路径params: {                  //params中的参数被拼接到url后面=》/demo/getTest?param=hiparam: "hi"              //post请求中后端使用@RequestParam接参数,使用params}}).then(res => {console.log(res)}).catch(err => {alert(err)})},}
}  
// axios等价于
export default {name: 'App',methods: {postTest: function () {this.$axios.post("/demo/postTest?param=hi") .then(res => {console.log(res)}).catch(err => {alert(err)}) },}
} 

get请求发送的请求时能使用被@RequestBody标记的对象接收。

//后端@RequestMapping(value = "/postTest2", method = RequestMethod.POST)public Map<String, Object> postTest2(@RequestBody Map param) {log.info("入参:{}", param);HashMap<String, Object> map = new HashMap<>(2);map.put("param", param);log.info("出参:{}", map);return map;}
//axios
export default {name: 'App',methods: {postTest: function () {this.$axios({method: "POST",            //方法url: "/demo/postTest2",     //路径data: {                  //data中的参数被放置在请求体当中,使用@RequestBody标记的对象接收param: "hi"       }}).then(res => {console.log(res)}).catch(err => {alert(err)})},}
}  

其他方法

delete(url: string);

export default {name: 'App',methods: {//第一种deleteTest: function () {this.$axios({method: "DELETE",url: "/demo/deleteTest",params: {param: "hi"    //同样使用params}}).then(res => {console.log(res)}).catch(err => {alert(err)})},//第二种deleteTest1: function () {this.$axios.delete("/demo/deleteTest?param=hi").then(res => {console.log(res)}).catch(err => {alert(err)})},}
}

head(url: string);

export default {name: 'App',methods: {headTest: function () {this.$axios({method: "HEAD",url: "/demo/headTest",params: {param: "hi"   //同样使用params}}).then(res => {console.log(res)}).catch(err => {alert(err)})},headTest1: function () {this.$axios.head("/demo/headTest?param=hi").then(res => {console.log(res)}).catch(err => {alert(err)})},}
}

options(url: string);

export default {name: 'App',methods: {optionsTest: function () {this.$axios({method: "OPTIONS",url: "/demo/optionsTest",params: {param: "hi"   //同样使用params}}).then(res => {console.log(res)}).catch(err => {alert(err)})},optionsTest1: function () {this.$axios.options("/demo/optionsTest?param=hi").then(res => {console.log(res)}).catch(err => {alert(err)})},}
}

put(url: string, data?: any);

export default {name: 'App',methods: {putTest: function () {this.$axios({method: "PUT",url: "/demo/putTest",data: {param: "hello"    //使用data和params都可以}                    //data放置在请求体,params拼接在请求路径}).then(res => {console.log(res)}).catch(err => {alert(err)})},putTest1: function () {this.$axios.put("/demo/putTest", {param: "hello"}).then(res => {console.log(res)}).catch(err => {alert(err)})},}
}

patch(url: string, data?: any);

export default {name: 'App',methods: {patchTest: function () {this.$axios({method: "PATCH",url: "/demo/patchTest",data: {param: "hello"     //使用data和params都可以}                    //data放置在请求体,params拼接在请求路径}).then(res => {console.log(res)}).catch(err => {alert(err)})},patchTest1: function () {this.$axios.patch("/demo/patchTest", {param: "hello"},).then(res => {console.log(res)}).catch(err => {alert(err)})},}
}
http://www.lryc.cn/news/464517.html

相关文章:

  • 智能EDA小白从0开始 —— DAY20 OrCAD
  • C# WebApi 接口测试工具:WebApiTestClient应用技术详解
  • Qt_ymode自己实现
  • 5.3章节python中字典:字典创建、元素访问、相关操作
  • ECCV2024 Tracking 汇总
  • C语言知识点
  • ICMP协议以及ARP欺骗攻击
  • qt5.12.12插件机制无法加载插件问题
  • 机器学习面试笔试知识点-线性回归、逻辑回归(Logistics Regression)和支持向量机(SVM)
  • SpringBoot民宿预订系统设计与实现
  • linux环境下C程序的编译过程以及makefile的简单使用
  • 【从零开始的LeetCode-算法】945. 使数组唯一的最小增量
  • Java程序设计:spring boot(2)
  • 服务器运维监控平台
  • css中 global 和 deep(两个样式穿透) 区别
  • 【星闪技术】WS63E模块的WiFi客户端测试
  • Android面试之5个Kotlin深度面试题:协程、密封类和高阶函数
  • 操作系统 和 初识进程
  • QT--Qlabel学习、获取文本和设置文本、文本对齐方式、文本换行、显示图片
  • 深度学习:终身学习(Life-Long Learning)详解
  • 前端UI框架
  • 最佳副屏串流解决方案:如何低成本打造电脑拓展副屏?
  • SQL Injection | SQL 注入概述
  • 【Linux 从基础到进阶】磁盘I/O性能调优
  • 浅谈AGI时代的“数据枢纽”——向量数据库
  • 生成 Excel 表列名称
  • 基于yolov10的烟雾明火检测森林火灾系统python源码+pytorch模型+评估指标曲线+精美GUI界面+数据集
  • UltraISO(软碟通)制作U盘制作Ubuntu20.04启动盘
  • 【EtherCAT实践篇一】TwinCAT 3安装、使用
  • 4、CSS3笔记