axios前端参数的传递几种方法

  1. 直接拼接url

    const axios = require('axios');
    
    // 假设有两个参数:id 和 category
    const id = 123;
    
    // 使用模板字符串将参数拼接在 URL 上
    axios.get(`https://api.xxx.com/data?id=${id}`)
      .then(response => {
        console.log(response.data);
      })
      .catch(error => {
        console.error(error);
      });
      
    // 直接作为URL的一部分
    axios.get(`https://api.xxx.com/data/${id}`)
      .then(response => {
        console.log(response.data);
      })
      .catch(error => {
        console.error(error);
      });
    
  2. 使用params传递参数

    const axios = require('axios');
    
    // 定义 params 对象
    const params = {
      id: 123,
    };
    
    // 将 params 对象传递给 GET 请求
    //axios.get('https://api.xxx.com/data', { params })
    axios.get('https://api.xxx.com/data', { params:params })
      .then(response => {
        console.log(response.data);
      })
      .catch(error => {
        console.error(error);
      });
    

资料参考:\node_modules\axios\index.d.ts

在这里插入图片描述在这里插入图片描述

相关推荐

  1. 前端下载图片方式

    2024-03-23 10:12:04       33 阅读
  2. ZeroMq传输视频方案

    2024-03-23 10:12:04       38 阅读
  3. 【pytest】pytest` 中常用参数方法

    2024-03-23 10:12:04       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 10:12:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 10:12:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 10:12:04       87 阅读
  4. Python语言-面向对象

    2024-03-23 10:12:04       96 阅读

热门阅读

  1. 阿里云DataWorks数据治理实践

    2024-03-23 10:12:04       38 阅读
  2. ES6—Module 的语法

    2024-03-23 10:12:04       42 阅读
  3. 前端小白的学习之路(ES6 三)

    2024-03-23 10:12:04       48 阅读
  4. FM25512

    2024-03-23 10:12:04       43 阅读
  5. 【单点知识】基于实例讲解PyTorch中的ImageFolder类

    2024-03-23 10:12:04       40 阅读
  6. 7-24 两个整数最大值

    2024-03-23 10:12:04       40 阅读
  7. 关于RestController发送请求用List<T> 接收数据

    2024-03-23 10:12:04       42 阅读
  8. ChatGPT PLUS 团队版 和 ChatGPT PLUS 比较

    2024-03-23 10:12:04       68 阅读
  9. Linux常用通配符

    2024-03-23 10:12:04       41 阅读