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

JavaScript中使用fetch发送POST请求如何正确添加参数

问题原因

原生 fetch API 不支持param/params配置项,你代码中写的param字段会被直接忽略,参数没有正确携带到请求中,所以和Postman的请求逻辑不一致导致报错。

根据你Postman的参数配置,对应两种常见写法:

场景1:参数是Postman中「Params」栏的查询参数(即拼接在URL?后面的参数)

先构造查询参数拼到请求URL上再发起请求:

// 构造查询参数
const searchParams = new URLSearchParams({
  token: token,
  id: id,
  id_secret: id_secret
})
// 拼接为完整请求地址
const requestUrl = `${url_ticket}?${searchParams.toString()}`

fetch(requestUrl, {
  method: 'POST',
  // 如果你还有请求体参数,对应打开下面注释配置即可
  // headers: {
  //   'Content-Type': 'application/json'
  // },
  // body: JSON.stringify(data)
})
.then(response => response.text())
.then(resText => console.log(resText))
.catch(error => console.error(error))

场景2:参数是Postman中「Body」栏的表单参数(x-www-form-urlencoded格式)

直接把参数放到请求体body中即可:

const formParams = new URLSearchParams({
  token: token,
  id: id,
  id_secret: id_secret
})

fetch(url_ticket, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: formParams
})
.then(response => response.text())
.then(resText => console.log(resText))
.catch(error => console.error(error))

额外注意

你注释掉的mode: 'no-cors'不要随便开启,开启后无法获取正常响应内容,也会看起来像是请求执行失败。

内容的提问来源于stack exchange,提问作者OK 400

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 00:36:02