Heroku,Rails 4和Rack :: Cors

编程入门 行业动态 更新时间:2024-10-17 02:47:57
本文介绍了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 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

我发现这个问题(无法让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

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

发布评论

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

>www.elefans.com

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