在构建 API 以了解它们的工作方式时,我在访问使用 HTML 表单将数据添加到底层数据库的端点时遇到了问题。
这是表单的 HTML:
<form action="mobile/add" method="post">
<legend>Add Mobile</legend>
<ol>
<li>
<label for="name">name</label>
<input type="text" name="name" id="name"></input>
</li>
<li>
<label for="model">model</label>
<input type="text" name="model" id="model"></input>
</li>
<li>
<label for="color">color</label>
<input type="text" name="color" id="color"></input>
</li>
</ol>
<button type="submit">Submit</button>
<button type="reset">Cancel</button>
</form>
记笔记action="mobile/add"
此表单设置为在此 URL 中可见:
http://localhost:8080/miscellaneous/APIexamples/CRUD/mobile/list单击提交时,数据将添加到数据库中,从列出移动数据的端点可以看出:
我相信重定向是由于action="mobile/add"表单中的,这是将数据添加到数据库所必需的,但不是我希望用户看到的。
有没有办法将表单中的数据添加到数据库中,但http://localhost:8080/miscellaneous/APIexamples/CRUD/在清除“添加移动”表单字段的情况下重新显示?或者上面使用的方法是非标准的(即,这通常是使用 AJAX 而不是表单提交来处理的)?
结果:
感谢@VoiceOfUnreason 的回答,将 HTTP 状态代码更改为 205 更正了问题。数据仍然提交,但页面没有重定向。但是,表单域没有被清除。为此,对表单标签进行了如下修改<form action="mobile/add" method="post" onsubmit="this.submit();this.reset();return false;">。这一点智慧来自@atesin 2019 年 8 月 17 日在这个帖子中的帖子。



