如何在rails下的webrick中设置access-control-allow-origin?
我编写了一个小型 Rails 应用程序,通过 xmlhttprequests 向另一个站点提供内容,该应用程序将从另一个域运行(不可能让它们在同一服务器上运行).我知道我需要在我的 rails 服务器上设置 access-control-allow-origin 以允许请求网页访问此材料.
I have written a small rails app to serve up content to another site via xmlhttprequests that will be operating from another domain (it will not be possible to get them running on the same server). I understand I will need to set access-control-allow-origin on my rails server to allow the requesting web page to access this material.
关于如何使用 Apache 执行此操作的文档似乎相当完备,这可能是我在部署站点后将使用的服务器.虽然我正在开发,但我希望只使用 webrick,就像我习惯使用 rails 一样.有没有办法配置 webrick 以在 rails 中提供适当的 http 标头?
It seems fairly well documented how to do this with Apache and this is probably the server I will use once I deploy the site. While I am developing though I hope to just use webrick as I am used to doing with rails. Is there a way of configuring webrick to provide the appropriate http header within rails?
推荐答案
如果您使用的是 Rails 2,只需将其添加到您的应用程序控制器.
If you're on Rails 2 just add this to your application contoller.
before_filter :set_access
def set_access
@response.headers["Access-Control-Allow-Origin"] = "*"
end
显然将 "*"
更改为不那么开放的东西是个好主意.
Obviously changing "*"
to something a little less open would be a good idea.
相关文章