【问题标题】:Rails - Controller that does not check for CSRF tokenRails - 不检查 CSRF 令牌的控制器
【发布时间】:2015-10-27 06:05:16
【问题描述】:

在我的 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.

我该如何解决这个问题?

【问题讨论】:

    标签: ruby-on-rails csrf


    【解决方案1】:

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

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

    Allow anything through CORS Policy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 2014-07-18
      • 2013-03-27
      • 2015-06-09
      • 2017-04-06
      • 2016-09-28
      • 2020-10-12
      相关资源
      最近更新 更多