05-AXIOS

入门案例

  • 入门步骤:https://github.com/typicode/json-server

1、创建一个 json-server 文件夹,并安装 软件包

npm install -g json-server

2、在文件夹下创建 db.json 文件

{
    "posts": [
        {
            "id": 1,
            "title": "json-server",
            "author": "typicode"
        }
    ],
    "comments": [
        {
            "id": 1,
            "body": "some comment",
            "postId": 1
        }
    ],
    "profile": {
        "name": "typicode"
    }
}

3、启动服务

npx json-server --watch db.json

4、引入axios

  • 可以通过https://www.bootcdn.cn 提高访问的速度
<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.2.5/axios.js"></script>

5、测试

<script>
        let btns = document.querySelectorAll('button');
        btns[0].addEventListener('click', () => {
            axios({
                method: 'GET',
                url: 'http://localhost:3000/posts'
            }).then(response => {
                console.log(response);
            })
        })

        btns[1].addEventListener('click', () => {
            axios({
                method: 'POST',
                url: 'http://localhost:3000/posts',
                data: {
                    "title": "robber",
                    "author": "typicode"
                }

            })
        })

        btns[2].addEventListener('click', () => {
            axios({
                method: 'PUT',
                url: 'http://localhost:3000/posts/2',
                data: {
                    "id": "2",
                    "title": "lkasdjfkljaskdlfjkl",
                    "author": "typicode"
                }
            })
        })

        btns[3].addEventListener('click', () => {
            axios({
                method: 'delete',
                url: 'http://localhost:3000/posts/2'
            }).then(response => {
                console.log(response);
            })
        })
    </script>s

axios 使用

常用语法

1、axios.request(config): 等同于 axios(config)

2、axios.get(url[, config]): 发 get 请求

3、axios.delete(url[, config]): 发 delete 请求

4、axios.post(url[, data, config]): 发 post 请求

axios.request(config): 等同于 axios(config)

btns[0].addEventListener('click', () => {
    axios.request({
        method: 'GET',
        url: 'http://localhost:3000/comments/1'
    }).then(response => {
        console.log(response);
    })
})

axios.post(url[, data, config]): 发 post 请求

btns[1].addEventListener('click', () => {
    axios.post(
        'http://localhost:3000/comments',
        {
            "body": "asdfasdfasdfsome comment",
            "postId": 2
        }).then(response => {
            console.log(response);
        })
})

默认设置

axios.defaults.method = 'GET'; //设置默认请求方式
        axios.defaults.baseURL = 'http://localhost:3000/comments'; //设置基础路径
        axios.defaults.params = { id: 100 }; //设置默认请求url参数
        btns[0].addEventListener('click', () => {
            axios.request({
                method: 'GET',
                url: '/1'
            }).then(response => {
                console.log(response);
            })
        })

创建实例对象发送请求

let btns = document.querySelectorAll('button');
let duanzi = axios.create({
    beaseURL: 'http://localhost:3000',
    timeout: 2000
});

duanzi({
    url: '/comments/1'
}).then(response => {
    console.log(response);
})

请求拦截器

<script>
    let btns = document.querySelectorAll('button');

    //设置请求拦截器
    axios.interceptors.request.use(function (config) {
        console.log('请求拦截器成功')
        return config;
    }, function (error) {
        console.log('请求拦截器失败');
        return Promise.reject(error);
    });

    //设置响应拦截器
    axios.interceptors.response.use(function (response) {
        console.log('响应拦截器成功');
        return response;
    }, function (error) {
        console.log('响应拦截器失败');
        return Promise.reject(error);
    });

    //设置操作
    axios({
        method: 'GET',
        url: 'http://localhost:3000/comments/1'
    }).then(respone => {
        console.log(respone.data);
    })

</script>

取消请求

<script>
    let btns = document.querySelectorAll('button');

    //临时变量
    let cancel = null;
    btns[0].addEventListener('click', () => {
        //用户已经点击则直接取消
        if (cancel !== null) {
            cancel();
        }
        axios({
            method: 'GET',
            url: 'http://localhost:3000/posts/1',
            //添加配置对象的属性
            cancelToken: new axios.CancelToken(function (c) {
                cancel = c;
            })
        }).then(response => {
            console.log(response);
            cancel = null;
        })

    })

    btns[1].addEventListener('click', () => {
        console.log(cancel());
    })
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值