HTMX POST请求表单有效,但未包括字段数据。

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

HTMX POST request for a form is working, but there's no field data included

问题

我有一个表单,应该提交一个包含表单字段数据的POST请求,但是当后端应用程序接收到请求时,它是空的(没有数据)。为什么它不起作用?

<form hx-post="/meme">
	<label for="name">有趣的迷因名称</label>
	<input type="text" id="name">

	<label for="rating">幽默等级</label>
	<input type="number" id="rating">

	<button type="submit">提交!</button>
</form>
英文:

I have a form that should submit a POST request containing the form field data, but when the backend app receives the request, its blank (no data). Why isn't it working?

<form hx-post="/meme">
	<label for="name">Funny meme name</label>
	<input type="text" id="name">

	<label for="rating">Funniness rating</label>
	<input type="number" id="rating">

	<button type="submit">Submit!</button>
</form>

答案1

得分: 1

表单字段缺少name属性。仅当表单字段设置了name属性时,它们才会包含在POST请求中。例如:name="funny_meme_name"

以下是已设置name属性的代码。

<form hx-post="/meme">
    <label for="name">Funny meme name</label>
    <input type="text" id="name" name="name">

    <label for="rating">Funniness rating</label>
    <input type="number" id="rating" name="rating">

    <button type="submit">Submit!</button>
</form>

另外,name应该是唯一的。如果有两个具有相同名称的字段,只有一个将在请求中发送。

英文:

The form fields are missing their name attributes. Form fields are only included in a POST request if they have a name attribute set. E.g. name="funny_meme_name".

Here's the code with name's set.

<form hx-post="/meme">
    <label for="name">Funny meme name</label>
    <input type="text" id="name" name="name">

    <label for="rating">Funniness rating</label>
    <input type="number" id="rating" name="rating">

    <button type="submit">Submit!</button>
</form>

Also, the name's should be unique. If there's two fields with the same name, only one will be sent in the request.

huangapple
  • 本文由 发表于 2023年5月17日 18:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271217.html
匿名

发表评论

匿名网友

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

确定