Heroku、Rails 4 和 Rack::Cors

编程入门 行业动态 更新时间:2024-10-17 07:31:56
本文介绍了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 jasonbutzinfo.herokuapp/messages.json --header 'Origin: 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 > Accept: */* > Origin: 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 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.

通过我的猴子补丁,我在 initialize、call 和 allow 方法中放置了 puts 语句.initialize 和 allow 方法都被调用.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

本文发布于:2023-11-24 20:42:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails   Heroku   Cors   Rack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!