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

  1. 直接拼接url

    js 复制代码
    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传递参数

    js 复制代码
    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

相关推荐
前端老石人3 分钟前
前端开发中的 URL 完全指南
开发语言·前端·javascript·css·html
CAE虚拟与现实3 分钟前
五一假期闲来无事,来个前段、后端的说明吧
前端·后端·vtk·three.js·前后端
Sarvartha14 分钟前
三目运算符
linux·服务器·前端
晓晨的博客21 分钟前
ROS1录制的bag包转换为ROS2格式
前端·chrome
Wect29 分钟前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·typescript
donecoding42 分钟前
别再让 pnpm 跟着 nvm 跑了!独立安装终极指南
前端·node.js·前端工程化
GISer_Jing44 分钟前
AI全栈转型_TS后端学习路线
前端·人工智能·后端·学习
竹林81844 分钟前
被The Graph的GraphQL查询坑了三天,我用一个真实DeFi项目把链上数据索引彻底搞懂了
前端·graphql
漫游的渔夫44 分钟前
前端开发者做 Agent:别只会执行,用 4 类失败策略让 AI 知道怎么停
前端·人工智能·typescript
用户059540174461 小时前
把多级缓存一致性验证从手工测试换成 Pytest 参数化,Bug 排查时间缩短 90%
前端·css