You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么同一个请求在Postman中正常运行,使用Axios发送时却失效?

Axios请求错误修复方案
  • 请求头配置字段拼写错误:axios 官方要求的请求头配置字段为headers(复数形式),你代码中使用了单数header,导致配置的Content-Type、auth-token都不会被挂载到实际发出的请求上,后端无法拿到鉴权信息,直接返回错误。
  • auth-token值格式错误:你代码中"auth-token": { token }的写法会给请求头传入一个对象类型的值,而接口要求的是纯token字符串,正确写法为"auth-token": token。Postman中你直接填写的是token字符串,所以请求可以正常执行。

修正后代码

await axios({
      method: "post",
      url: `https://ecommersappbytim.herokuapp.com/info/${userId}/update-user`,
      headers: {
        "Content-Type": "application/json",
        "auth-token": token,
      },
      data: {
        firstName: userInfo.fName,
        lastName: userInfo.lName,
        email: userInfo.email,
        password: userInfo.password,
      },
    })

额外说明:当前主流版本的axios发送JSON格式的POST请求时,会自动设置Content-Type: application/json,无需手动配置该请求头。

内容的提问来源于stack exchange,提问作者Tim Taylor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 13:21:05