英文:
Remix Run + React Framework: Form submission is blank
问题
一直在尝试测试Remix JS框架的表单提交,但似乎无法使其工作。我一定是漏掉了什么明显的东西,但已经盯着屏幕几个小时了,还是找不到原因:
下面是我看到的截图... 我有一个非常简单的表单,其中有一个名为"name"的输入文本字段。当我提交它时,我可以看到表单数据包括name: test
,但当我在服务器日志中执行console.log(body)
时,我看到FormData {}。
有谁知道为什么会出现这种不一致情况吗?
英文:
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?
答案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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论