Heroku,Rails 4和Rack :: Cors [英] Heroku, Rails 4, and Rack::Cors

查看:131
本文介绍了Heroku,Rails 4和Rack :: Cors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Rack :: Cors与我的Rails 4应用程序,以便我可以做一个基于JSON的API。

I am trying to use Rack::Cors with my Rails 4 application so that I can do a JSON based API.

CORS是在我的Gemfile像这样:

CORS is in my Gemfile like this:

gem 'rack-cors', :require => 'rack/cors'

我在我的application.rb文件中进行配置, p>

I am doing the configuration in my application.rb file like this:

config.middleware.insert_after Rails::Rack::Logger, Rack::Cors, :debug => true, :logger => Rails.logger do
    allow do
        origins '*'
        resource '/messages*', :headers => :any, :methods => [:post, :options]
    end
end

Rails :: Rack :: Logger 尝试获取调试信息。

我使用CURL测试,这里是我一直在运行:

I am using CURL to test it, here is what I have been running:

curl --verbose --request OPTIONS http://jasonbutzinfo.herokuapp.com/messages.json --header 'Origin: http://www.jasonbutz.info' --header 'Access-Control-Request-Headers: Origin, Accept, Content-Type' --header 'Access-Control-Request-Method: POST'



当我在本地计算机上运行rails应用程序时,它没有问题。当我打开Heroku应用程序,这是我得到:

When I run the rails app on my local machine it works without issue. When I hit the Heroku app this is what I get:

> OPTIONS /messages.json HTTP/1.1
> User-Agent: curl/7.30.0
> Host: jasonbutzinfo.herokuapp.com
> Accept: */*
> Origin: http://www.jasonbutz.info
> Access-Control-Request-Headers: Origin, Accept, Content-Type
> Access-Control-Request-Method: POST
> 
* Empty reply from server
* Connection #0 to host jasonbutzinfo.herokuapp.com left intact
curl: (52) Empty reply from server

我发现这个问题(无法让rails-cors在rails应用程序中工作),但没有提供任何有用的答案。

I did find this question (Can't get rack-cors working in rails application), but there wasn't any helpful answer provided.

更新11/13/2013 16:40 EST

我一直在试着做更多的调试工作。我有猴子修补了几个Rack :: Cors的方法,看看他们是否甚至被呼吁Heroku。

I've been trying to do some more debugging with what is going on. I have monkey patched a few of Rack::Cors' methods to see if they are even being called on Heroku. I have also changed where I insert Cors to be at the top of the rack middleware stack.

使用我的猴子修补程序,我已经把 puts

With my monkey patching I have put puts statements in the initialize, call, and allow methods. The initialize and allow methods are both called. The call method is never called. So it seems there is something that is stopping the request before it gets to the cors middleware.

推荐答案

看起来问题是是由我的机器或网络引起的。我已经SSHed到一个托管环境我使用和使用curl命令上面和它工作。

It looks like the issue is being caused by my machine or the network I am on. I SSHed into a hosting environment I use and used the curl command above and it worked.

附加注意
这是别的刚刚发生,我想我应该添加到这。我的AJAX请求不是我的Heroku应用程序的https URL,但Heroku翻译为https。这导致了额外的跨源问题。切换为使用https作为AJAX请求固定此。

Additional Note Here is something else that just happened that I thought I ought to add to this. My AJAX request was not to the https URL for my Heroku app, but Heroku was translating it be https. This was causing an additional cross-origin issue. Switching to use https for the AJAX request fixed this.

这篇关于Heroku,Rails 4和Rack :: Cors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆