0

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 リクエストから何も返しません。なぜこれが起こるのでしょうか??

4

1 に答える 1

2

実際のjson文字列ではなく、「this.json」を部分的にレンダリングしようとしています。

format.json {render :json=> "this.json"}代わりにすべきではありませんか?

于 2012-04-26T19:12:47.237 に答える