javascript - 使用 x-www-form-urlencoded 内容类型将嵌套对象发布为 formdata

标签 javascript post form-data x-www-form-urlencoded

我必须发送 header 内容类型设置为“x-www-form-urlencoded”的 post 方法的数据。

这个表单数据也是嵌套对象。 例如

const formData = { name: "hello", email:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="29484b4a694e44484045074a4644" rel="noreferrer noopener nofollow">[email protected]</a>, education: { subject: "engilsh" ... } } }

最佳答案

您可以使用querystring模块。

像这样类似 Express 的伪代码一样发布数据:

const querystring = require('querystring');

// ...

router.post(
  'https://api/url',
  querystring.stringify(formData),
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
)

//编辑:querystring 模块不适用于嵌套对象。我的错。我也许建议将对象序列化为 JSON 字符串。

关于javascript - 使用 x-www-form-urlencoded 内容类型将嵌套对象发布为 formdata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63524369/

相关文章:

php - HttpPost 500 错误。(简单代码)

php - 将其中包含引号的字符串作为不带反斜杠的 post 方法发送

javascript - 在 ASP.NET 表单中提交更改的 FormData,以便通过 ModelState 和重定向实现正常行为

node.js - 当我在 Postman 中使用表单数据时,结果未定义

javascript - 我们如何从 Javascript File 对象知道文件的内容?

javascript - 使用javascript自动计算具有来自mysql的数据的表行

javascript - 如何拼接字典javascript

javascript - 谷歌图表从主数据数组中删除数组

javascript - Next.js - 理解 getInitialProps

ios - YouTube Api 发送 POST 请求