如何在rails下的webrick中设置access-control-allow-origin?

How to set access-control-allow-origin in webrick under rails?(如何在rails下的webrick中设置access-control-allow-origin?)

本文介绍了如何在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.

这篇关于如何在rails下的webrick中设置access-control-allow-origin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在rails下的webrick中设置access-control-allow-origin?

基础教程推荐