仍然获得“Access-Control-Allow-Origin不允许使用Origin MYSITE.com”。错误。
在我的控制器里我有
def subscribe
gibbon = Gibbon::Request.new
@list_id = 'some_id'
begin
resp = gibbon.lists(@list_id).members.create({body: subscribe_params})
render json: {}, status: 200
rescue Gibbon::MailChimpError => e
render json: e.body, status: e.body['status']
end
end
在我的config / application.rb
中config.middleware.insert_before 0, "Rack::Cors", :debug => true, :logger => (-> {Rails.logger }) do
allow do
origins(/http:\/\/localhost:(\d*)/,
/http:\/\/.*\.MYSITE\.com/,
/http:\/\/MYSITE\.com/,
)
resource '*',
:headers => :any,
:methods => [:get, :post, :delete, :put, :options, :head, :patch],
:max_age => 0
end
end
在我的routes.rb中我有
namespace :api, defaults: {format: 'json'} do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: :true) do
resources :mailers do
collection do
post :subscribe
get :members
end
end
match '*path', via: [:options], to: lambda {|_| [204, {'Content-Type' => 'text/plain'}, []]}
end
end
在我的nginx>网站启用> MYSITE.con
try_files $uri/index.html $uri @unicorn;
location @unicorn {
add_header 'Access-Control-Allow-Origin' '*';
add_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
答案 0 :(得分:1)
对于遇到类似问题的其他人来说,问题在于nginx conf。
需要删除add_header 'Access-Control-Allow-Origin' '*';
起初没有注意到Access-Control-Allow-Origin
响应标头返回http://MYSITE, *
无效。只允许1个来源。