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

查看:15
本文介绍了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 文件中进行配置:

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 之后插入以尝试获取调试信息.

I am inserting after Rails::Rack::Logger in an attempt to get debugging information.

我正在使用 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

我确实发现了这个问题(无法获得rack-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 更新

我一直在尝试对正在发生的事情进行更多的调试.我让猴子修补了一些 Rack::Cors 的方法,看看它们是否甚至在 Heroku 上被调用.我还更改了插入 Cors 的位置,使其位于机架中间件堆栈的顶部.

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.

通过我的猴子补丁,我在 initializecallallow 方法中放置了 puts 语句.initializeallow 方法都被调用.call 方法永远不会被调用.所以似乎有什么东西在请求到达 cors 中间件之前停止了请求.

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.

推荐答案

问题似乎是由我的机器或我所在的网络引起的.我通过 SSH 连接到我使用的托管环境并使用了上面的 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.这导致了一个额外的跨域问题.切换到对 AJAX 请求使用 https 解决了这个问题.

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天全站免登陆