• axios案例


     使用说明:参考 https://www.kancloud.cn/yunye/axios/234845

     跨域解决:参考 https://blog.csdn.net/u012369271/article/details/72848102

     使用心得:参考 https://mp.weixin.qq.com/s/Gnv2LVY8AHIBT7Z5Iq5RXg

      以下是简单demo截图:

        

         

         

         

         

       以下是:案例源码

    <!DOCTYPE html>
    <html>

    <head>
    <meta charset="UTF-8">
    <title>axiosdemo</title>
    </head>

    <body>
    <button onclick="getaxios()">getaxios</button>
    <button onclick="postaxios()">postaxios</button>
    <button onclick="comaxios()">comaxios</button>
    <button onclick="allaxios()">allaxios</button>
    <button onclick="cusAxios()">cusAxios</button>
    <button onclick="interceptor()">interceptor</button>

    </body>

    <!-- 引入axios -->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <script>
    function interceptor() {
    var instance = axios.create({
    url: './axios.php',
    timeout: 3000,
    method: 'post',
    params: {
    type: 'top'
    }
    });
    instance.interceptors.request.use(config => {
    console.log(111);
    // 设置一些加载动画等操作
    return config;
    });
    // .get .post
    instance.request().then(function(data) {
    console.log(data);
    }).catch();
    }

    function cusAxios() {
    var instance = axios.create({
    url: './axios.php',
    timeout: 3000,
    method: 'post',
    params: {
    type: 'top'
    }
    });
    instance.get();
    instance.request();
    }

    // 多个并发请求
    function allaxios() {
    function getAxios1() {
    return axios.get('./axios.php?type=top');
    }

    function getAxios2() {
    return axios.get('./axios.php?type=yule');
    }

    axios.all([getAxios1(), getAxios2()]).then(axios.spread(function(data1, data2) {
    console.log(data1);
    console.log(data2);
    }))
    }

    function comaxios() {
    // 通过config对象来配置axios请求
    axios({
    method: 'post',
    url: './axios.php',
    data: {
    type: 'top'
    }
    })
    .then(function(response) {
    console.log(response);
    })
    .catch(function() {

    })
    }

    function postaxios() {
    axios.post('./axios.php', {
    type: 'top'
    })
    .then(function(response) {
    console.log(response);
    })
    .catch(function() {})
    }

    function getaxios() {
    axios.get('./axios.php', {
    params: {
    type: 'top'
    }
    })
    .then(function(response) {
    console.log(response);
    })
    .catch(function(err) {

    });
    }
    </script>

    </html>

      

  • 相关阅读:
    make_shared和shared_ptr的区别
    bitcoined
    ofstream的使用方法--超级精细。C++文件写入、读出函数(转)
    visual studio 安装过程
    Socket通信原理探讨(C++为例)
    以太坊的记录和关注点
    以太坊的应用
    以太坊
    比特币、以太坊
    c++
  • 原文地址:https://www.cnblogs.com/TuringShine/p/11601842.html
Copyright © 2020-2023  润新知