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

axios初入门

1,axiox的基本使用

<!doctype 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>

2,默认配置

<!doctype 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>
    </div>
    <script>
        //获取按钮
        const btns = document.querySelectorAll('button');
        //默认配置
        axios.defaults.method = 'GET';//设置默认的请求类型为 GET
        axios.defaults.baseURL = 'http://localhost:3000';//设置基础 URL
        axios.defaults.params = {id:100};
        axios.defaults.timeout = 3000;//

        btns[0].onclick = function(){
            axios({
                url: '/posts'
            }).then(response => {
                console.log(response);
            })
        }

    </script>
</body>

</html>

3,设置拦截器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="axios.js"></script>
</head>
<body>
<script>
    //设置请求拦截器
    axios.interceptors.request.use(function (config) {
        console.log("请求拦截成功")
        config.params={a:100}
        return config
    },function (error) {
        return Promise.reject(error)
    })
    //设置响应
    axios.interceptors.response.use(function (response) {
        console.log("响应成功")
        return response.data;
    },function (error) {
        console.log("响应器拦截失败 失败1号")
        return Promise.reject(error)
    })
    //发送请示
    axios({
        method:"GET",
        url:"http://localhost:3000/posts"
    }).then(response=>{
        console.log("自定义回调处理成功的结果")
        console.log(response)
    })
</script>

</body>
</html>

4,取消请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>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",
            cancelToken:new axios.cancelToken(function (c) {
                cancel=c;
            })
        }).then(response=>{
            console.log(response)
            cancel=null
        })
        //绑定第二个事件取消请求
        btns[1].onclick=function () {
            cancel();
        }


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

5,axios的二次封装

未完待继...

相关文章:

  • hivesql 将json格式字符串转为数组
  • 基于OGG实现Oracle实时同步MySQL
  • kafka入门(二): 位移提交
  • 大数据预处理技术
  • 从word复制内容到wangEditor富文本框的时候会把html标签也复制过来,如果只想实现直接复制纯文本,有什么好的实现方式
  • ElasticSearch学习笔记(狂神说)
  • AIGC创作系统ChatGPT网站源码、支持最新GPT-4-Turbo模型、GPT-4图片对话能力+搭建部署教程
  • 使用DeepBlueCLI对Windows日志进行取证(小记)
  • ChatGPT初体验:注册、API Key获取与ChatAPI调用详解
  • 力扣6:N字形变化
  • Java王者荣耀
  • pytest-pytest-html测试报告这样做,学完能涨薪3k
  • JAVA小游戏“简易版王者荣耀”
  • MYSQL 及 SQL 注入
  • 企业数字化转型的作用是什么?_光点科技
  • HuggingFace学习笔记--Tokenizer的使用
  • MySQL表的操作『增删改查』
  • 基于xml配置的AOP
  • 面对Spring 不支持java8的改变方法
  • 网络安全深入学习第九课——本机信息收集
  • 大外交|中美联合声明拉升全球股市,专家:中美相向而行为世界提供确定性
  • 哈佛新论文揭示 Transformer 模型与人脑“同步纠结”全过程!AI也会犹豫、反悔?
  • 中国工程院院士、国医大师石学敏逝世
  • 新华时评:中国维护国际经贸秩序的立场坚定不移
  • 婚姻登记“全国通办”首日,上海虹口登记中心迎来“甜蜜高峰”
  • 1450亿元!财政部拟发行2025年中央金融机构注资特别国债(二期)