欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > Axios 快速入门

Axios 快速入门

2025/6/20 23:01:14 来源:https://blog.csdn.net/Broken_x/article/details/142729467  浏览:    关键词: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 的用法将极大地提升开发效率。

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词