[英]Should I use form to submit JSON data to REST api
Is it approved to use a html form to build interface to collect user data, convert it to domain model in front end and POST it to server using JSON? 是否批准使用html表单构建接口以收集用户数据,将其转换为前端的域模型并使用JSON将其POST到服务器?
I like to think the user input part as a logical form, event though the server expects JSON. 我喜欢将用户输入部分视为逻辑形式,事件虽然服务器需要JSON。 For user it is all the same, so why not just to use a form to collect the data and implement a custom onSubmit method?
对于用户来说它是完全相同的,那么为什么不只是使用表单来收集数据并实现自定义的onSubmit方法呢? Another option would be use detached input fields and buttons.
另一种选择是使用分离的输入字段和按钮。
Is this considered a good practice to use forms for JSON POST and what are the good and bad consequences of this practice? 这被认为是使用JSON POST表单的好习惯,这种做法有哪些好的和坏的后果?
Here is my thought on this: 以下是我的想法:
Pros: 优点:
Cons: 缺点:
I see a lot more cons thant pros, but maybe your situation truly justify this? 我看到更多的专业人士,但也许你的情况真的证明了这一点?
Grouping inputs and submit buttons within a <form>
is semantic and good practice, it simply requires calling preventDefault
on the event passed to your onSubmit
handler to prevent the hard form submit. 在
<form>
输入和提交按钮进行分组是语义和良好实践,它只需要在传递给onSubmit
处理程序的事件上调用preventDefault
来阻止硬表单提交。 Your API should speak JSON, and it's trivial to save your form values into state values into state onBlur/onChange, and use jQuery or some other lib to create a JSON post. 您的API应该说JSON,将表单值保存到状态值为onBlur / onChange并使用jQuery或其他一些lib来创建JSON帖子是微不足道的。 While its not completely necessary, I cannot think of drawbacks.
虽然它不是完全必要的,但我不能想到缺点。 Fallbacks for the "no javascript" scenario aren't realistic now for most use cases.
对于大多数用例,“无javascript”场景的后备现在都不现实。
i think its a bad approach. 我认为这是一个糟糕的方法。
in client side, do client logic, ui logic, etc... in server side do your processing. 在客户端,做客户端逻辑,ui逻辑等...在服务器端进行处理。
also in many API you will need credentials/token...you sure you wanna share those on cliend scripts (: ?? 在许多API中你还需要凭证/令牌......你确定要在cliend脚本上分享那些(:??
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.