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

Java课题笔记~ Axios

Axios 对原生的AJAX进行封装,简化书写。

Axios官网是:https://www.axios-http.cn

2.1 基本使用

axios 使用是比较简单的,分为以下两步:

  • 引入 axios 的 js 文件

<script src="js/axios-0.18.0.js"></script>

使用axios 发送请求,并获取响应结果

  • 发送 get 请求

axios({method:"get",url:"http://localhost:8080/ajax-demo1/aJAXDemo1?username=zhangsan"
}).then(function (resp){alert(resp.data);
})
  • 发送 post 请求
axios({method:"post",url:"http://localhost:8080/ajax-demo1/aJAXDemo1",data:"username=zhangsan"
}).then(function (resp){alert(resp.data);
});

axios() 是用来发送异步请求的,小括号中使用 js 对象传递请求相关的参数:

  • method 属性:用来设置请求方式的。取值为 get 或者 post

  • url 属性:用来书写请求的资源路径。如果是 get 请求,需要将请求参数拼接到路径的后面,格式为: url?参数名=参数值&参数名2=参数值2

  • data 属性:作为请求体被发送的数据。也就是说如果是 post 请求的话,数据需要作为 data 属性的值。

then() 需要传递一个匿名函数。我们将 then() 中传递的匿名函数称为 回调函数,意思是该匿名函数在发送请求时不会被调用,而是在成功响应后调用的函数。而该回调函数中的 resp 参数是对响应的数据进行封装的对象,通过 resp.data 可以获取到响应的数据。

2.2 快速入门

2.2.1 后端实现

定义一个用于接收请求的servlet,代码如下:

@WebServlet("/axiosServlet")
public class AxiosServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("get...");//1. 接收请求参数String username = request.getParameter("username");System.out.println(username);//2. 响应数据response.getWriter().write("hello Axios~");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("post...");this.doGet(request, response);}
}

2.2.2 前端实现

  • 引入 js 文件

<script src="js/axios-0.18.0.js"></script>

发送 ajax 请求

  • get 请求

axios({method:"get",url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"
}).then(function (resp) {alert(resp.data);
})
  • post 请求
axios({method:"post",url:"http://localhost:8080/ajax-demo/axiosServlet",data:"username=zhangsan"
}).then(function (resp) {alert(resp.data);
})

整体页面代码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><script src="js/axios-0.18.0.js"></script>
<script>//1. get/* axios({method:"get",url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"}).then(function (resp) {alert(resp.data);})*///2. post  在js中{} 表示一个js对象,而这个js对象中有三个属性axios({method:"post",url:"http://localhost:8080/ajax-demo/axiosServlet",data:"username=zhangsan"}).then(function (resp) {alert(resp.data);})
</script>
</body>
</html>

2.3 请求方法别名

为了方便起见, Axios 已经为所有支持的请求方法提供了别名。如下:

  • get 请求 : axios.get(url[,config])

  • delete 请求 : axios.delete(url[,config])

  • head 请求 : axios.head(url[,config])

  • options 请求 : axios.option(url[,config])

  • post 请求:axios.post(url[,data[,config])

  • put 请求:axios.put(url[,data[,config])

  • patch 请求:axios.patch(url[,data[,config])

而我们只关注 get 请求和 post 请求。

入门案例中的 get 请求代码可以改为如下:

axios.get("http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan").then(function (resp) {alert(resp.data);
});

入门案例中的 post 请求代码可以改为如下:

axios.post("http://localhost:8080/ajax-demo/axiosServlet","username=zhangsan").then(function (resp) {alert(resp.data);
})

http://www.lryc.cn/news/134208.html

相关文章:

  • ip地址和地理位置有关系吗
  • mac指定node版本 mac node版本降级 mac切换node版本
  • C# Windows登录界面进行截图,控制鼠标键盘等操作实现(一)
  • 因果推断(五)基于谷歌框架Causal Impact的因果推断
  • VR全景加盟项目如何开展?如何共赢VR时代红利?
  • Win10+anaconda+CUDA+pytorch+vscode配置
  • vue-router在vue2/3区别
  • Apache Doris 入门教程33:统计信息
  • 有效需求的特征
  • 基于51单片机无线温度报警控制器 NRF24L01 多路温度报警系统设计
  • Spring Data JPA的@Entity注解
  • CANoe panel中,Path Dialog如何保存选择的文件路径
  • 关于es中索引,倒排索引的理解
  • k8s service (二)
  • 桌面软件开发框架 Electron、Qt、WPF 和 WinForms 怎么选?
  • SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录(第二天)Mybatis的深入学习
  • 学习笔记:Opencv实现限制对比度得自适应直方图均衡CLAHE
  • R语言处理缺失数据(1)-mice
  • SpringBoot自动配置原理
  • HarmonyOS学习路之方舟开发框架—学习ArkTS语言(状态管理 五)
  • Java基础篇——反射枚举
  • 每日一学——案例难点Windows配置
  • 2023.8 - java - 运算符
  • 推荐三款Scrum敏捷项目管理工具/敏捷管理实践
  • WARNING: undefined behavior - version of Delve is too old for Go version
  • https非对称加密算法
  • “深入探索JVM:Java虚拟机背后的奥秘“
  • 树莓派系统入门教程(三)—— 使用Windows上的VSCode远程连接树莓派进行Python开发
  • 如何使用HTML5新增的标签来优化SEO?
  • LVS之keepalived