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

Vue中Axios POST触发FastAPI 307临时重定向问题求助

问题修复方案

核心错误点及修正:

  1. 无需手动JSON.stringify序列化
    Axios 的 post 方法会自动将传入的 JavaScript 对象序列化为 JSON,并设置正确的 Content-Type: application/json 请求头,和你用 Python requests.post(..., json=data) 的行为完全一致。手动调用 JSON.stringify 反而会让 Axios 把字符串当成普通表单数据处理,导致后端无法解析请求体,触发307重定向。

  2. 数组转字符串的写法错误
    JavaScript 里没有全局的 toString([1,2,3]) 用法,要生成和 Python str([1,2,3]) 一致的 "[1,2,3]" 字符串,需要用 JSON.stringify([1,2,3])。

修正后的Vue代码:

axios.post('http://127.0.0.1:8000/events/v1/items', {
  page_id: "v1",
  useragent: "Mozilla/5.0",
  ip_adress: "1.1.1.1",
  age: 31,
  question: 1,
  answer_array: JSON.stringify([1,2,3]),
  other_answer: ""
})
.then(response => console.log(response))
.catch(error => console.log(error))

额外排查点:

如果修正后仍出现307,检查 FastAPI 的路由定义:

  • 如果后端路由是 @app.post("/events/v1/items/")(末尾带斜杠),请把请求 URL 改成 http://127.0.0.1:8000/events/v1/items/(加斜杠),或者修改后端路由去掉末尾斜杠。

内容的提问来源于stack exchange,提问作者Sergei Suslov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 15:35:21