Ruby on rails 在rails中,如何从另一个url中的json获取内容

Ruby on rails 在rails中,如何从另一个url中的json获取内容,ruby-on-rails,json,ajax,rest,Ruby On Rails,Json,Ajax,Rest,您好,我在rails中创建了一个项目,其中我有一个返回json的api,我想获得这个json的内容,并在我的视图中展示如何以优雅的方式完成它。我是用ajax实现的,但我想知道是否有任何方法仅在rails中实现这一点 谢谢你的关注,我需要说英语不是我的主要语言 我使用下面的代码来提供我所需要的 <script language="JavaScript"> $(document).ready(function(){ $('#people').click(function(){

您好,我在rails中创建了一个项目,其中我有一个返回json的api,我想获得这个json的内容,并在我的视图中展示如何以优雅的方式完成它。我是用ajax实现的,但我想知道是否有任何方法仅在rails中实现这一点

谢谢你的关注,我需要说英语不是我的主要语言

我使用下面的代码来提供我所需要的

<script language="JavaScript">
$(document).ready(function(){

$('#people').click(function(){

    $.ajax({
        url: "http://localhost:3000/people.json",
        //force to handle it as text
        dataType: "text",
        success: function(data) {

            //data downloaded so we call parseJSON function 
            //and pass downloaded data
            var json = $.parseJSON(data);
            //now json variable contains data in json format
            //let's display a few items

            for (var i=0;i<json.length;++i)
            {
                $('#response').append('<div class="name">'+ json[i].name +  json[i].age + json[i].gender + json[i].lonlat + json[i].name +'</>');
            }
        }
    });

});

});
</script>

$(文档).ready(函数(){
$(“#人”)。单击(函数(){
$.ajax({
url:“http://localhost:3000/people.json",
//强制将其作为文本处理
数据类型:“文本”,
成功:功能(数据){
//下载的数据,因此我们称之为parseJSON函数
//并传递下载的数据
var json=$.parseJSON(数据);
//现在json变量包含json格式的数据
//让我们展示几个项目
对于(var i=0;i您可以使用:


另一种方法是创建一个
js.erb
模板,但这更糟。

谢谢,我会照你说的做。:)
<%= link_to "Something", something_path, id: "people", data: { remote: true, type: 'json' } %>
// Put this in application.js or somewhere else in app/assets/javascripts
// not inline. 
$(document).on('ajax:success', '#people', function(data, status){
  var $el = $('#response');

  $.each(data, function(p){
     $el.append(
       '<div class="name">'+ p.name + p.age + p.gender + p.lonlat + p.name +'</div>'
     );
  }); 
});