0
点赞
收藏
分享

微信扫一扫

[Javaweb]Axios异步框架


文章目录

  • ​​概述​​
  • ​​Axios基本使用​​
  • ​​Axios请求方式别名​​

概述

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

[Javaweb]Axios异步框架_ios

特性:

  • 从浏览器中创建 XMLHttpRequests
  • 从 node.js 创建 http 请求
  • 支持 Promise API
  • 拦截请求和响应
  • 转换请求数据和响应数据
  • 取消请求
  • 自动转换 JSON 数据
  • 客户端支持防御 XSRF

这一篇文章我们主要使用Axios对原生Ajax的封装特性

Axios基本使用

Axios的使用也很简单:

[Javaweb]Axios异步框架_数据_02

第一步:我们先把Axios下载下来

npm install axios

当然我们不能直接在web项目中引入一个本地资源,我们可以把里面的核心文件拖入到项目中

第二步:使用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​​ 可以获取到响应的数据。

我们简单的使用Axios发送一个请求,将请求参数打印的同时对服务器返回的数据进行呈现:
服务器端:

@WebServlet("/Axios1")
public class ServletAxiosDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("你发送了一个post请求");
String username = request.getParameter("username");
System.out.println(username);
response.getWriter().write("hello.Axios~");
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("你发送了一个get请求");
String username = request.getParameter("username");
System.out.println(username);
response.getWriter().write("hello.Axios~");
}
}

前端:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../lib/axios.min.js"></script>
</head>
<body>

</body>

<script>axios({
method:"get",
url:"http://localhost:8080/tomcat-demo1/Axios1?username=zs"
}).then((res) => {
alert(res.data)
})

axios({
method:"post",
url:"http://localhost:8080/tomcat-demo1/Axios1",
data:"username=zs"
}).then((res) => {
alert(res.data)
})</script>
</html>

结果:

[Javaweb]Axios异步框架_前端_05


[Javaweb]Axios异步框架_ios_06

Axios请求方式别名

为了方便起见, 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])​

注意
在使用别名方法时, url、method、data 这些属性都不必在配置中指定。

我们拿get、post举例:

[Javaweb]Axios异步框架_数据_07

例如我们上面代码中的:

axios({
method:"get",
url:"http://localhost:8080/tomcat-demo1/Axios1?username=zs"
}).then((res) => {
alert(res.data)
})

可以变为:

axios.get("http://localhost:8080/tomcat-demo1/Axios1?username=zs").then((res) => {
alert(res.data)
})

axios({
method:"post",
url:"http://localhost:8080/tomcat-demo1/Axios1",
data:"username=zs"
}).then((res) => {
alert(res.data)
})

可以变为:

.post("http://localhost:8080/tomcat-demo1/Axios1","username=zs").then((res) => {
alert(res.data)
})

两者的优缺点:
使用别名的方式阅读性较差,但代码简洁


举报

相关推荐

0 条评论