How to set access-control-allow-origin in webrick under rails?(如何在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?


基础教程推荐
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01