简体   繁体   English

我应该使用表单将REST数据提交给REST API

[英]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: 优点:

  1. Potentially you can re-use web services to perform form validation as well as acting as a standalone service. 您可以重复使用Web服务来执行表单验证以及充当独立服务。 Make sure you really have this need before implementing this non-standard method. 在实现这种非标准方法之前,请确保您确实有此需求。

Cons: 缺点:

  1. You are moving away from standard form/post models which is a basic feature of HTML for several years. 您正在逐渐远离标准表单/帖子模型,这是HTML的基本功能多年。
  2. Your users will be forced to use JavaScript - not such a big deal in 2016 but you are losing accessibility for... what gain? 您的用户将被迫使用JavaScript - 在2016年没有那么大的优惠,但您正在失去可访问性......获得了什么?
  3. You will have to manage this requirement for multiple browsers (adds potential complexity). 您将不得不为多个浏览器管理此要求(增加了潜在的复杂性)。
  4. Possibly the page load rank will be slower depending on your implementation. 页面加载等级可能会慢一些,具体取决于您的实现。 This might impact SEO and user experience. 这可能会影响SEO和用户体验。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM