Ruby on rails 什么';rails将表单数据发布到第三方web服务的方式是什么?
我有一个简单的表单,其中有一个文本区域,它将文本区域中的字符串作为JSON提交给外部web服务。以下是提交表单之前的情况: 以下是表单提交后的外观: 下面是创建表单的html.erb代码:Ruby on rails 什么';rails将表单数据发布到第三方web服务的方式是什么?,ruby-on-rails,json,forms,jquery,ruby-on-rails-4,Ruby On Rails,Json,Forms,Jquery,Ruby On Rails 4,我有一个简单的表单,其中有一个文本区域,它将文本区域中的字符串作为JSON提交给外部web服务。以下是提交表单之前的情况: 以下是表单提交后的外观: 下面是创建表单的html.erb代码: <script type="text/javascript"> function call_service() { var data_to_send = document.getElementById("data_to_send").value; $.ajax({
<script type="text/javascript">
function call_service() {
var data_to_send = document.getElementById("data_to_send").value;
$.ajax({
url : "http://externalservice.com",
data : data_to_send,
type : "POST",
dataType : "json",
contentType: "application/json"
}).done(function(data) {
// populate the table
}).error(function(data) {
// handle error
});
}
$(document).ready(function() {
$("#table_div").hide();
});
</script>
<!-- heading html redacted -->
<!-- <%= form_tag("http://externalservice.com",
method: :post,
remote: true,
role: "form",
data: "json") do %> -->
<form role="form">
<h4 class="form-heading">Enter Data:</h4>
<%= text_area_tag("data_to_send", @data,
rows: 5,
class: "form-control",
style: "font-family: Monaco;") %>
<p class="help-block">
Here you can edit the JSON data or click "Generate" to just
use the data that's already there.
</p>
<%= button_to_function("Generate Table!", "call_service(); this.blur()" ,class: "btn btn-primary") %>
<!-- <% end %> -->
</form>
<!-- table html redacted -->
函数调用_服务(){
var data_to_send=document.getElementById(“data_to_send”).value;
$.ajax({
url:“http://externalservice.com",
数据:要发送的数据,
类型:“POST”,
数据类型:“json”,
contentType:“应用程序/json”
}).完成(功能(数据){
//填充表格
}).错误(函数(数据){
//处理错误
});
}
$(文档).ready(函数(){
$(“#table_div”).hide();
});
输入数据:
在这里,您可以编辑JSON数据,或单击“生成”以
使用已经存在的数据。
注释掉的form_标签
就是我在决定使用jQuery和按钮to_函数
位使事情正常运行之前所要做的
我的问题是,如何以rails的方式完成同样的事情——一种向外部服务发送JSON数据的表单?无论是使用
remote:true
表单还是其他形式?如果第三方Web服务符合REST,您可能需要尝试一下
还可以阅读更多提示。web服务不是一个成熟的资源。我真正感兴趣的是它的返回时间是否足够长,以便在同一页的表中显示它。大多数情况下,我只是好奇是否有更多的rails方法来完成我手工破解的Ajax调用。不过,感谢您提供有关ActiveResource的提示!