Rails - 不检查CSRF令牌的控制器

时间:2015-08-04 17:50:57

标签: ruby-on-rails csrf

在我的rails应用程序中,其中一个控制器显示公共统计信息,我希望托管在不同域上的网站从中提取数据。 (app.donornest.com/stats.json)

我的控制器代码如下:

class StatsController < ApplicationController 
require 'ostruct'
skip_before_action :verify_authenticity_token
respond_to :html, :xml, :json, :csv

def index
    @stats = OpenStruct.new
    @stats.users = User.all.count
    @stats.organizations = Organization.all.count
    @stats.donors = Person.all.count
    respond_to do |format|
        format.json {render json: @stats}
    end
end
end

我认为行skip_before_action:verify_authenticity_token就足够了,但是当我尝试从控制台向此页面发出请求时,我收到以下错误:

XMLHttpRequest cannot load http://app.donornest.com/stats.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

这看起来不像是真实性令牌错误。

看起来像跨域访问控制错误。检查此答案以在启用CORS的客户端的rails中启用访问控制头:

Allow anything through CORS Policy