简体   繁体   English

从Rails表单发送JSON数据

[英]sending json data from rails form

I have a rails form, which has some fields. 我有一个rails表单,其中包含一些字段。

Now i want to send values of these fields, converted to json to some url 现在我想发送这些字段的值,将其转换为json到某些网址

I can traverse through each element of form, convert it into json(using javascript) prepare a json string and send a request to the server.[client side conversion] 我可以遍历表单的每个元素,将其转换为json(使用javascript),准备一个json字符串,然后向服务器发送请求。[客户端转换]

Other approach can be, posting the form normally to some controller action. 其他方法可以是将表单正常发布到某些控制器操作中。 then reading the data on the rails controller. 然后在rails控制器上读取数据。 parsing it to json and sending to desired url[server side conversion] 将其解析为json并发送至所需的url [服务器端转换]

But i dont think these are good approaches. 但是我不认为这些是好的方法。

what can be better way to do this? 有什么更好的方法可以做到这一点?

Why do you want to convert before sending to the server? 为什么要在发送到服务器之前进行转换? You can easily generate JSON in Rails on the server side using the submitted POST data. 您可以使用提交的POST数据在服务器端的Rails中轻松生成JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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