访问字体"字体网址"来自Origin" Url"已被阻止CORS错误

时间:2018-04-19 22:35:57

标签: ruby-on-rails cors

您好我好好看看并且无法找到答案。

错误非常清楚,但解决方案不是。

未加载自定义字体。我在assets/fonts/TypoGraphica.eot

下有自定义字体

以下是错误:

enter image description here

font.scss

@font-face {
  font-family:"TypoGraphica";
  src:  font-url("TypoGraphica.eot?") format("eot"),
        font-url("TypoGraphica.woff") format("woff"),
        font-url("TypoGraphica.ttf") format("truetype"),
        font-url("TypoGraphica.svg#TypoGraphica") format("svg");
}

.typographica { font-family: $font-typographica; }

这是我的角色。目前我允许任何请求:

module Taddar
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', headers: :any, methods: [:get, :post, :options]
      end
    end

    config.active_job.queue_adapter = :delayed_job
    config.browserify_rails.commandline_options =
      '-t [ babelify --presets [ env react stage-0 ] --plugins [ syntax-async-functions transform-regenerator ] ]'
  end
end 

有谁知道我做错了什么?

2 个答案:

答案 0 :(得分:0)

来自其他来源

对于我的同伴来说,这就是我最终解决这个问题的方法。问题不在于gem本身,问题是堆栈没有提供OPTIONS路由,就像ruby之外的任何其他框架一样自动(是吗?)如果你习惯使用Java或类似的东西,这个可能会让你毫无准备。

因此,在routes.rb中添加这些路由可以解决问题:

  # global options responder -> makes sure OPTION request for CORS endpoints work
  match '*path', via: [:options], to: lambda {|_| [204, { 'Content-Type' => 'text/plain' }]}

答案 1 :(得分:0)

通过将以下内容添加到assets.rb

解决了此问题

Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf)$/