我正在尝试使用omniauth将我的客户端定向到外部服务授权页面。我的客户端使用ember.js,我的服务器是rails服务器。事实上,我能够在我的服务器端轻松打电话,但我的客户端不会重定向,给我一个错误的阅读
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404.
做一些研究表明我需要使用已经安装和配置的CORS。现在我只需要知道将access-control-allow-origin
标头应用于我的代码的位置和方式。任何人都可以帮我解决这个问题吗?
我的路线:
get 'auth/:provider/callback' => 'sessions#create'
我的会话控制器:
class SessionsController < ApplicationController
def create
@request.env
auth = request.env['omniauth.auth']
Account.recieve_donation(auth)
end
end
答案 0 :(得分:0)
以下是您必须执行的操作如果您要进行GET请求,您的客户端请求必须发送 Origin标头,然后您的服务器必须发送 Access-Control-Allow-响应中的Origin标题。 这两个标头的值必须相同才能允许跨源资源共享。
答案 1 :(得分:0)
我找到了一个ruby gem Rack CORS Middleware来解决'Access-control-allow-origin'标题问题。
如果您不想使用ruby gem,请在控制器中添加以下代码(application_controller,other_name_controller)
before_action :allow_cross_domain_access
after_action :cors_set_access_control_headers
def allow_cross_domain_access
headers['Access-Control-Allow-Origin'] = '*'# http://localhost:9000
headers['Access-Control-Allow-Headers'] = 'GET, POST, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Methods'] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(',')
headers['Access-Control-Max-Age'] = '1728000'
end
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(',')
headers['Access-Control-Max-Age'] = "1728000"
end