我编写了一个小型 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Angular 2 (Ionic 2):拦截 ajax 请求Angular 2 (Ionic 2): intercept ajax requests(Angular 2 (Ionic 2):拦截 ajax 请求)
如何在 Ionic 中使用 CORS 编写 Angular 2 服务?How do I write an Angular 2 service with CORS in Ionic?(如何在 Ionic 中使用 CORS 编写 Angular 2 服务?)
Angular 2 HTTP 进度条Angular 2 HTTP Progress bar(Angular 2 HTTP 进度条)
从 IIS 中的应用程序打印到服务器上的网络打印机Printing from an application in IIS to a networked printer on server(从 IIS 中的应用程序打印到服务器上的网络打印机)
angular2 使用 HTTP 发布 XML 类型的请求数据angular2 posting XML type request data using HTTP(angular2 使用 HTTP 发布 XML 类型的请求数据)
舍入经纬度以在 Google 地图中显示大致位置Rounding Lat and Long to Show Approximate Location in Google Maps(舍入经纬度以在 Google 地图中显示大致位置)