⭐️⭐️⭐️ 作者:船长在船上
🚩🚩🚩 主页:来访地址船长在船上的博客
🔨🔨🔨 简介:资深前端开发工程师,专注前端开发,欢迎咨询交流,共同学习
🔔🔔🔔 感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主,如果三连收藏支持就会更好,在这里博主不胜感激!!!如有疑问可以留言、评论,看到后会及时回复。


- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>axios基本使用title>
- <link crossorigin="anonymous" href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js">script>
- head>
- <body>
- <div class="container">
- <h2 class="page-header">基本使用h2>
- <button class="btn btn-primary"> 发送GET请求 button>
- <button class="btn btn-warning" > 发送POST请求 button>
- <button class="btn btn-success"> 发送 PUT 请求 button>
- <button class="btn btn-danger"> 发送 DELETE 请求 button>
- div>
- <script>
- //获取按钮
- const btns = document.querySelectorAll('button');
-
- //第一个
- btns[0].onclick = function(){
- //发送 AJAX 请求
- axios({
- //请求类型
- method: 'GET',
- //URL
- url: 'http://localhost:3000/posts/2',
- }).then(response => {
- console.log(response);
- });
- }
-
- //添加一篇新的文章
- btns[1].onclick = function(){
- //发送 AJAX 请求
- axios({
- //请求类型
- method: 'POST',
- //URL
- url: 'http://localhost:3000/posts',
- //设置请求体
- data: {
- title: "今天天气不错, 还挺风和日丽的",
- author: "张三"
- }
- }).then(response => {
- console.log(response);
- });
- }
-
- //更新数据
- btns[2].onclick = function(){
- //发送 AJAX 请求
- axios({
- //请求类型
- method: 'PUT',
- //URL
- url: 'http://localhost:3000/posts/3',
- //设置请求体
- data: {
- title: "今天天气不错, 还挺风和日丽的",
- author: "李四"
- }
- }).then(response => {
- console.log(response);
- });
- }
-
- //删除数据
- btns[3].onclick = function(){
- //发送 AJAX 请求
- axios({
- //请求类型
- method: 'delete',
- //URL
- url: 'http://localhost:3000/posts/3',
- }).then(response => {
- console.log(response);
- });
- }
-
- script>
- body>
-
- html>
1.根据指定配置创建一个新的 axios, 也就就每个新 axios 都有自己的配置
2.新 axios 只是没有取消请求和批量发请求的方法, 其它所有语法都是一致的
3.为什么要设计这个语法?
1. 说明: 调用 axios()并不是立即发送 ajax 请求, 而是需要经历一个较长的流程
2. 流程: 请求拦截器2 => 请求拦截器1 => 发ajax请求 => 响应拦截器1 => 响
应拦截器 2 => 请求的回调
3. 注意: 此流程是通过 promise 串连起来的, 请求拦截器传递的是 config, 响应
拦截器传递的是 response
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>拦截器title>
- <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js">script>
- head>
- <body>
- <script>
- // Promise
- // 设置请求拦截器 config 配置对象
- axios.interceptors.request.use(function (config) {
- console.log('请求拦截器 成功 - 1号');
- //修改 config 中的参数
- config.params = {a:100};
-
- return config;
- }, function (error) {
- console.log('请求拦截器 失败 - 1号');
- return Promise.reject(error);
- });
-
- axios.interceptors.request.use(function (config) {
- console.log('请求拦截器 成功 - 2号');
- //修改 config 中的参数
- config.timeout = 2000;
- return config;
- }, function (error) {
- console.log('请求拦截器 失败 - 2号');
- return Promise.reject(error);
- });
-
- // 设置响应拦截器
- axios.interceptors.response.use(function (response) {
- console.log('响应拦截器 成功 1号');
- return response.data;
- // return response;
- }, function (error) {
- console.log('响应拦截器 失败 1号')
- return Promise.reject(error);
- });
-
- axios.interceptors.response.use(function (response) {
- console.log('响应拦截器 成功 2号')
- return response;
- }, function (error) {
- console.log('响应拦截器 失败 2号')
- return Promise.reject(error);
- });
-
- //发送请求
- axios({
- method: 'GET',
- url: 'http://localhost:3000/posts'
- }).then(response => {
- console.log('自定义回调处理成功的结果');
- console.log(response);
- });
- script>
- body>
- html>
1. 基本流程
2. 实现功能
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>取消请求title>
- <link crossorigin='anonymous' href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js">script>
- head>
- <body>
- <div class="container">
- <h2 class="page-header">axios取消请求h2>
- <button class="btn btn-primary"> 发送请求 button>
- <button class="btn btn-warning" > 取消请求 button>
- div>
- <script>
- //获取按钮
- const btns = document.querySelectorAll('button');
- //2.声明全局变量
- let cancel = null;
- //发送请求
- btns[0].onclick = function(){
- //检测上一次的请求是否已经完成
- if(cancel !== null){
- //取消上一次的请求
- cancel();
- }
- axios({
- method: 'GET',
- url: 'http://localhost:3000/posts',
- //1. 添加配置对象的属性
- cancelToken: new axios.CancelToken(function(c){
- //3. 将 c 的值赋值给 cancel
- cancel = c;
- })
- }).then(response => {
- console.log(response);
- //将 cancel 的值初始化
- cancel = null;
- })
- }
-
- //绑定第二个事件取消请求
- btns[1].onclick = function(){
- cancel();
- }
- script>
- body>
- html>
下一篇文章:
👉👉👉 欢迎来访船长在船上的博客,如有疑问可以留言、评论,看到后会及时回复。