Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何从远程应用程序获取Json数据_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 如何从远程应用程序获取Json数据

Ruby on rails 如何从远程应用程序获取Json数据,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我的rails应用程序中有一个呈现json数据的控制器 def show @visit = Visit.all @count = @visit.first.count unless @visit == [] respond_to do |format| format.html # new.html.erb format.xml { render :xml => @visit.first } format.json {render :partial => "thi

我的rails应用程序中有一个呈现json数据的控制器

def show
 @visit = Visit.all
 @count = @visit.first.count unless @visit == [] 
 respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @visit.first }
  format.json {render :partial => "this.json"}
 end
end
我在我的另一个应用程序中执行了一个简单的ajax请求,以从我的视图中获取json字符串

$.ajax({
url: 'http://url.com/show.json', 
success: function( data ){
    console.log(data);
  $('#data').html(data);
 }
});​
对于跨域标头,我使用Rack Cors

use Rack::Cors do
 allow do
  origins '*'
  # regular expressions can be used here
  resource '/countvisit/*', :headers => :any, :methods => :any
  # headers to expose
 end
end

我不会拒绝ajax请求的任何内容。知道为什么会发生这种情况吗?

您正在尝试将'this.json'呈现为部分,而不是实际的json字符串


它不应该是
format.json{render:json=>“this.json”}
吗?

您的部分文件命名正确吗_this.json.erb?现在我得到了无法加载的XMLHttpRequest。访问控制不允许原点允许原点。我对跨域文件做了什么错误吗感谢所有人的回复,我通过在url末尾添加回调并获得jsonP来修复它,因此现在我能够获得值。大家好,我再次使用jsonP解决了这个问题,但它只在JSFIDLE中起作用。在我的主机中,我仍然得到跨域错误。知道为什么会发生这种情况吗?当您使用浏览器访问/show.json时,您看到了什么?正如@ismaelga所指出的,您的部分hds将在发布的第一个代码中命名为
\u this.json
。获得更多信息后将更新我的当前答案