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

相关推荐
吹什么轩4 分钟前
constexpr 完全指南:从 const 的两种含义,到编译期计算的四次演进
java·前端·javascript
Bruce_Liuxiaowei38 分钟前
前端校验登录态,等于没校验——一次应急广播终端高危漏洞的发现与报送
运维·前端·安全
Meta Luo44 分钟前
AI 时代,Web 自动化测试该怎么做?
前端·人工智能·playwright 替代工具·无代码自动化测试工具·低代码 ui 自动化测试·web 回归测试工具
去伪存真1 小时前
AI Agent 性能评估指标与自动化测试落地方案
前端·agent
MaAidan1 小时前
用 Agent 排查线上问题:SLS 日志分析 + DB/Redis 数据验证,一条链路搞清楚
前端·bootstrap·html
墨林陌1 小时前
AI 热点日报(2026-11-03):美团发布 1.6 万亿参数国产大模型,四大厂商共推 AI 训练退出标准
前端
Moment2 小时前
DeepSeek 发布新模型,直接把 Agent 联网搜索的门槛打下来了!
前端·后端
AI宇宙狂想家2 小时前
开源 AI 视频工具进化史:从难用到好用,OpenMontage 改了什么
开发语言·前端·人工智能·开源·ai创业·openmontage
Sailing2 小时前
🚀🚀【封闭结束7】Agent 为什么总是“失忆”?从长期记忆到上下文工程的生产实践
前端·人工智能·agent
OpenTiny社区2 小时前
AI 生成的 Vue 代码,终于能直接用了!
前端·vue.js·github