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

Axios介绍

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

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

基本使用

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

    • 发送 get 请求

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

      发送 post 请求

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

    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

     

发送 ajax 请求

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

  • post 请求
    axios({method: "post",url: "http://localhost:8080/ajax_demo/ajaxServlet",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="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 请求代码可以改为如下

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.xdnf.cn/news/16982.html

相关文章:

  • 达梦数据库备份与还原终极指南:从基础到增量策略实战
  • k8s+isulad 国产化技术栈云原生技术栈搭建4-添加worker节点
  • 使用Database Navigator插件进行连接sqlite报错invalid or incomplete database
  • 新电脑上GitHub推送失败?全面排查与解决指南
  • 力扣经典算法篇-41-旋转图像(辅助数组法,原地旋转法)
  • 基于深度学习的医学图像分析:使用变分自编码器(VAE)实现医学图像生成
  • 华为智能家居与Spring人工智能
  • PyTorch生成式人工智能(24)——使用PyTorch构建Transformer模型
  • 06.Redis 配置文件说明
  • C++ <type_traits> 应用详解
  • 需求和测试的映射关系
  • 推荐一款进程间高速交换数据的解决方案
  • 前端JS-调用单删接口来删除多个选中文件
  • 操作系统——读者写者问题
  • Spring **${}** vs **#{}** 语法全景图
  • 【C++ 初级工程师面试--5】inline内联函数特点 、和普通函数的区别、什么时候适合内联?
  • Shell脚本-变量如何定义
  • 什么是DOM和BOM?
  • 搜索引擎评估革命:用户行为模型如何颠覆传统指标?
  • 数据结构1-概要、单向链表
  • [网安工具] Web 漏洞扫描工具 —— AWVS · 使用手册
  • 【C语言】内存函数与数据在内存中的存储
  • python -m build打包成为tar.gz或者whl
  • Qemu-NUC980(二):时钟clock代码添加
  • Redis数据库存储键值对的底层原理
  • SpringBoot相关注解
  • #Linux内存管理#缺页中断处理的核心函数是do_page_fault()的工作原理
  • Vulnhub ELECTRICAL靶机复现(附提权)
  • RPG增容2.尝试使用MMC根据游戏难度自定义更改怪物属性(三)
  • (LeetCode 面试经典 150 题) 138. 随机链表的复制 (哈希表)