Axios介绍

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

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

基本使用

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

    • 发送 get 请求

      java 复制代码
      axios({
          method:"get",
          url:"http://localhost:8080/ajax-demo1/ajaxDemo?username=zhangsan"
      }).then(function (resp){
          alert(resp.data);
      })

      发送 post 请求

      java 复制代码
      axios({
          method:"post",
          url:"http://localhost:8080/ajax-demo1/ajaxDemo",
          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 可以获取到响应的数据。

快速入门

后端实现
  • 定义一个用于接收请求的servlet,代码如下:

    java 复制代码
    @WebServlet("/ajaxServlet")
    public class AjaxServlet extends HttpServlet {
        @Override
        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");
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("post...");
            this.doGet(request, response);
        }
    }
前端实现
  • 引入 js 文件

    java 复制代码
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

发送 ajax 请求

  • get 请求

    java 复制代码
    axios({
        method: "get",
        url: "http://localhost:8080/ajax_demo/ajaxServlet?username=zhangsan"
    }).then(function (resp) {
        alert(resp.data);
    })
  • post 请求

    java 复制代码
    axios({
        method: "post",
        url: "http://localhost:8080/ajax_demo/ajaxServlet",
        data: "username=zhangsan"
    }).then(function (resp) {
        alert(resp.data);
    })
  • 整体页面代码如下:

java 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    // axios({
    //     method: "get",
    //     url: "http://localhost:8080/ajax_demo/ajaxServlet?username=zhangsan"
    // }).then(function (resp) {
    //     alert(resp.data);
    // })

    axios({
        method: "post",
        url: "http://localhost:8080/ajax_demo/ajaxServlet",
        data: "username=zhangsan"
    }).then(function (resp) {
        alert(resp.data);
    })

</script>
</body>
</html>

分别测试get请求和post请求,

  • get请求会在页面上出现一个alert弹窗显示HELLO AXIOS,同时控制台输出

get...

zhangsan

  • post请求会在页面上出现一个alert弹窗显示HELLO AXIOS,同时控制台输出

post...

get...

zhangsan

请求方法别名

为了方便起见, 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 请求代码可以改为如下

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

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

java 复制代码
axios.post("http://localhost:8080/ajax-demo/axiosServlet","username=zhangsan").then(function (resp) {
    alert(resp.data);
})
相关推荐
crmscs1 分钟前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
localbob4 分钟前
杀戮尖塔 v6 MOD整合版(Slay the Spire)安卓+PC端免安装中文版分享 卡牌肉鸽神作!杀戮尖塔中文版,电脑和手机都能玩!杀戮尖塔.exe 杀戮尖塔.apk
android·杀戮尖塔apk·杀戮尖塔exe·游戏分享
机建狂魔8 分钟前
手机秒变电影机:Blackmagic Camera + LUT滤镜包的专业级视频解决方案
android·拍照·摄影·lut滤镜·拍摄·摄像·录像
hudawei9968 分钟前
flutter和Android动画的对比
android·flutter·动画
lxysbly2 小时前
md模拟器安卓版带金手指2026
android
儿歌八万首3 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
消失的旧时光-19435 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
Jinkxs5 小时前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin
&有梦想的咸鱼&5 小时前
Kotlin委托机制的底层实现深度解析(74)
android·开发语言·kotlin
LDORntKQH6 小时前
基于深度强化学习的混合动力汽车能量管理策略 1.利用DQN算法控制电池和发动机发电机组的功率分配 2
android