Remix Run + React 框架:表单提交为空白。

huangapple go评论73阅读模式
英文:

Remix Run + React Framework: Form submission is blank

问题

一直在尝试测试Remix JS框架的表单提交,但似乎无法使其工作。我一定是漏掉了什么明显的东西,但已经盯着屏幕几个小时了,还是找不到原因:

下面是我看到的截图... 我有一个非常简单的表单,其中有一个名为"name"的输入文本字段。当我提交它时,我可以看到表单数据包括name: test,但当我在服务器日志中执行console.log(body)时,我看到FormData {}。

有谁知道为什么会出现这种不一致情况吗?

Remix Run + React 框架:表单提交为空白。

英文:

Have been trying to test out the Remix JS framework's form submission and cannot seem to get it to work. I must be missing something obvious, but have been staring at it for hours and haven't been able to find it:

Below is a screenshot of what I'm seeing... I've got a very simple form that has one input text field called "name". When I submit it, I can see that the Form Data includes name: test, but then on my server logs, I see FormData {} when I do console.log(body).

Does anyone know why there is this inconsistency?

Remix Run + React 框架:表单提交为空白。

答案1

得分: 2

request.formData() 返回一个 FormData 对象

您需要使用像 get()getAll() 这样的 getter 方法。

export async function action({ request }: ActionArgs) {
    const form = await request.formData()
    const name = form.get('name')
    console.log(name)

    return json({ status: 'success' })
}

如果您查看 Remix 文档中的 action 部分,您可以看到他们使用 body.get('title') 来获取表单中的标题字段。

英文:

request.formData() returns a FormData object

You need to use getters such as get() or getAll().

export async function action({ request }: ActionArgs) {
    const form = await request.formData()
    const name = form.get('name')
    console.log(name)

    return json({ status: 'success' })
}

If you see the Remix docs on action, you can see they use body.get('title') to get the title field from the form.

答案2

得分: 0

不 ❌

console.log(formData)

是 ✅

console.log(formData.get("inputName"))

参考链接

英文:

Don't ❌

console.log(formData)

Do ✅

console.log(formData.get("inputName"))

<small>Reference</small>

huangapple
  • 本文由 发表于 2023年1月9日 17:52:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定