蓝桥杯练习——神秘咒语——axios

目标

完善 index.js 中的 TODO 部分,通过新增或者修改代码,完成以下目标:

点击钥匙 1 和钥匙 2 按钮时会通过 axios 发送请求,在发送请求时需要在请求头中添加 Authorization 字段携带 tokentoken 的值为 2b58f9a8-7d73-4a9c-b8a2-9f05d6e8e3c7

完成后效果如下所示:

完成效果

题解 

TODO:新增或者修改以下代码

key1Button.addEventListener('click', async () => {
    // 从后台请求钥匙1的咒语部分
    key1Button.disabled = true;
    let {data} =  await axios.get('/spellone',{
        headers:{
            'Authorization':'2b58f9a8-7d73-4a9c-b8a2-9f05d6e8e3c7',
        }
    })
    spell1.innerHTML = data;
    tryOpenTreasureBox();
});

key2Button.addEventListener('click', async () => {
    // 从后台请求钥匙2的咒语部分
    key2Button.disabled = true;
    let {data} =  await axios.get('/spelltwo',{
        headers:{
            Authorization:'2b58f9a8-7d73-4a9c-b8a2-9f05d6e8e3c7',
        }
    })
    spell2.innerHTML = data;
    tryOpenTreasureBox();
});

知识点补充:在JavaScript中使用axios发送请求的基本模板如下所示:

// 导入axios
const axios = require('axios');

// 设置请求的配置项(可选)
const config = {
  headers: {
    'Content-Type': 'application/json', // 设置请求头,根据需要修改
    // 其他配置项...
  },
  // 其他配置项...
};

// 发送 GET 请求
axios.get('http://api.example.com/data')
  .then(response => {
    // 处理响应数据
    console.log(response.data);
  })
  .catch(error => {
    // 处理错误
    console.error('Error fetching data: ', error);
  });

// 发送 POST 请求
const postData = {
  key1: 'value1',
  key2: 'value2',
  // 其他数据...
};

axios.post('http://api.example.com/post-endpoint', postData, config)
  .then(response => {
    // 处理响应数据
    console.log(response.data);
  })
  .catch(error => {
    // 处理错误
    console.error('Error posting data: ', error);
  });

// 其他请求类型的发送类似,如PUT、DELETE等,使用对应的axios方法即可

相关推荐

  1. 练习题

    2024-03-23 05:42:06       37 阅读
  2. 练习-简单1

    2024-03-23 05:42:06       35 阅读
  3. 练习-简单2

    2024-03-23 05:42:06       28 阅读
  4. 客观题练习笔记

    2024-03-23 05:42:06       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-23 05:42:06       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-23 05:42:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-23 05:42:06       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-23 05:42:06       20 阅读

热门阅读

  1. 在 CentOS 7 上编译安装 Nginx 1.18

    2024-03-23 05:42:06       22 阅读
  2. centos docker 安装es

    2024-03-23 05:42:06       16 阅读
  3. 在CentOS中怎么安装和配置NginxWeb服务器

    2024-03-23 05:42:06       18 阅读
  4. .NET 面试题

    2024-03-23 05:42:06       19 阅读
  5. vim | vim添加map快捷映射

    2024-03-23 05:42:06       19 阅读
  6. 【vim 学习系列文章 16 -- vim 自动保存设置】

    2024-03-23 05:42:06       17 阅读
  7. vim分屏命令

    2024-03-23 05:42:06       21 阅读
  8. Flutter-excel导入多语言脚本使用步骤

    2024-03-23 05:42:06       22 阅读
  9. react|设置环境变量存储密钥

    2024-03-23 05:42:06       23 阅读
  10. React 中 setState 更新状态的两种写法

    2024-03-23 05:42:06       21 阅读