代码之家  ›  专栏  ›  技术社区  ›  quazar

gem“rack cors”不工作,角度客户端请求rails api抛出cors错误

  •  0
  • quazar  · 技术社区  · 6 年前

    我正在努力让gem“rack cors”在当地工作。我用gem'devieve_token_auth'安装了裸rails,用'angular token'安装了angular 7客户端。但安格尔的每一个要求都是投球失误。我已经按照文档配置了rails和rack cors,但它不起作用。这是我的配置 application.rb

    config.middleware.insert_before 0, Rack::Cors do
          allow do
            origins '*'
            resource '*',
              headers: :any,
              expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
              methods: [:get, :post, :options, :delete, :put]
          end
        end
    

    如果我列出中间商使用 rails middleware , Rack::Cors 排在第二位 use Webpacker::DevServerProxy 但每个请求都会引发以下错误。

    Processing by DeviseTokenAuth::SessionsController#create as HTML
      Parameters: {"session"=>{}}
    HTTP Origin header (http://localhost:4200) didn't match request.base_url (http://localhost:3000)
    Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
    
    
    
    ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
    
    1 回复  |  直到 6 年前
        1
  •  -1
  •   quazar    6 年前

    事实证明,我需要改变如何保护 docs

    protect_from_forgery with: :null_session
    

    注:对于删除答案的人,请不要这样做,知道不该做什么也是学习的一部分。

    推荐文章