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

Axios 快速入门

什么是Ajax

        Ajax 是一种通过 JavaScript 发送异步请求的技术,它的核心是使用 XMLHttpRequest 对象来与服务器交换数据。这种方式较为繁琐,因为需要手动处理请求状态和响应,并且编写的代码往往比较冗长。

        相较之下,Axios 是一个基于 Promises 的 HTTP 客户端,简化了发送请求的过程,使得处理 HTTP 请求更加直观和便捷。Axios 封装了 XMLHttpRequest,并提供了一系列功能,例如请求和响应拦截、自动转换 JSON 数据等。

同步与异步 

在编程中,异步和同步是两种处理操作的方式:

  • 同步(Synchronous):在同步操作中,代码会按照顺序逐行执行,后面的代码必须等待前面的代码执行完成,这样会导致页面阻塞。

  • 异步(Asynchronous):异步操作允许代码在等待的同时继续执行其他任务。当任务完成后,会通过回调函数或 Promise 等机制处理结果。这种方式使得用户体验更加流畅,在进行网络请求时尤其重要。

        Axios 是一个专注于异步请求的库,通过使用 Promise,让开发者能够更容易地处理异步操作。例如,下面是使用 Axios 发送 GET 请求的代码:

axios.get('https://jsonplaceholder.typicode.com/todos/1')
    .then((result) => {
        console.log(result.data);
    })
    .catch((error) => {
        console.error(error);
    });

在这个例子中,通过 then() 和 catch() 方法,我们可以清晰地处理请求成功与失败的情况。 

Axios

通过Axios发送异步请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Axios入门</title>
    <script src="js/axios.js"></script>
</head>
<body>

    <input type="button" value="发送请求get" onclick="get()">
    <input type="button" value="发送请求post"onclick="post()">


    <script>
        //通过axios发送异步get请求
        function get() {
            axios({
                method: 'get',
                url: 'https://jsonplaceholder.typicode.com/todos/1'
            }).then((result) => {
                console.log(result.data);   
            })
        }

        //通过axios发送异步post请求
        function post() {
            axios({
                method: 'post',
                url: 'https://jsonplaceholder.typicode.com/todos',
            }).then((result) => {
                console.log(result.data);
            })
        }


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

Axios发送异步请求

        Axios 提供了多种发送网络请求的方法,最常见的包括 GET 和 POST 请求。使用 Axios 可以非常简单地发送请求:

1. 发送 GET 请求

发送 GET 请求的方式非常简单,只需调用 axios.get() 方法。

function get() {
    axios.get('https://jsonplaceholder.typicode.com/todos/1')
        .then((result) => {
            console.log(result.data);
        })
        .catch((error) => {
            console.error(error);
        });
}

2. 发送 POST 请求

        发送 POST 请求时,通过 axios.post() 方法可以轻松实现。可以在方法中传递数据作为第二个参数

function post() {
    axios.post('https://jsonplaceholder.typicode.com/todos', {
        title: '新任务',
        completed: false
    })
    .then((result) => {
        console.log(result.data);
    })
    .catch((error) => {
        console.error(error);
    });
}

在这个例子中,我们发送了一个包含任务标题和状态的 POST 请求,并处理了响应。

比较推荐的是用箭头函数和回调函数这种写法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Axios入门</title>
    <script src="js/axios.js"></script>
</head>
<body>

    <input type="button" value="发送请求get" onclick="get()">
    <input type="button" value="发送请求post"onclick="post()">


    <script>
        //通过axios发送异步get请求
        function get() {
            /* axios({
                method: 'get',
                url: 'https://jsonplaceholder.typicode.com/todos/1'
            }).then((result) => {
                console.log(result.data);   
            }) */

            axios.get('https://jsonplaceholder.typicode.com/todos/1').then((result) => {
                console.log(result.data);
            })
        }

        //通过axios发送异步post请求
        function post() {
            // axios({
            //     method: 'post',
            //     url: 'https://jsonplaceholder.typicode.com/todos',
            // }).then((result) => {
            //     console.log(result.data);
            // })

            axios.post('https://jsonplaceholder.typicode.com/todos').then((result) => {
                console.log(result.data);
            })
        }


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

总结

        Axios 作为一个现代化的 HTTP 客户端库,相较于传统的 AJAX 技术,无论在语法还是功能上都有了显著的提升。它使得与服务器的异步交互变得更加容易,特别是在处理大量网络请求时,不仅提高了代码的可读性,也减轻了维护的难度。对于 Web 开发者来说,了解并掌握 Axios 的用法将极大地提升开发效率。

 


http://www.kler.cn/news/337695.html

相关文章:

  • 本地访问autodl的jupyter notebook
  • 架构师必须多维度理解架构:视点、视角、视图(附PPT:华为企业架构数据、应用、技术架构设计方法论)
  • MySQL存储过程原理、实现及优化
  • Thinkphp/Laravel基于WEB技术的试卷生成在线考试系统 Vscode毕业设计成品源码_9hcr7
  • qemu-system-aarch64开启user用户模式网络连接
  • 旋转位置编码Rope
  • leetcode经典算法题总结
  • SpringBoot整合异步任务执行
  • micropython中断处理程序设计-临界区
  • 区间覆盖(贪心)
  • <Rust>iced库(0.13.1)学习之部件(三十一):picklist部件的使用及可变style设置
  • 自动驾驶传感器系列—自动驾驶中的“眼睛”:摄像头技术详解
  • springboot整合seata
  • 【代码配置】Orienting Point Clouds with Dipole Propagation
  • Redis 中热 Key 的判定及其解决方案
  • 传热学一些“数”和意义
  • 停车位识别数据集 图片数量12416张YOLO,xml和txt标签都有; 2类类别:space-empty,space-occupied;
  • 如何解决Lenovo笔记本电脑很快就自动休眠,自动锁屏,需要密码登录的问题
  • 激波是什么?
  • MySQL多表查询案例