如何在rails-api中响应OPTIONS HTTP方法? [英] How to respond to OPTIONS HTTP Method in rails-api?
问题描述
我正在使用rails-api构建公共的json API.
I'm using rails-api to build a public json api.
我想对OPTIONS HTTP方法做出响应,以利用跨域资源共享的优势.
I would like to respond to OPTIONS HTTP Method to take advantages of Cross-Origin Resource Sharing.
我正在这样做:
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
它可以工作,但是某些URL不支持所有HTTP方法.在那种情况下,我在撒谎.
It works, but some URLs does not support all HTTP methods. In those cases I'm lying.
我不想为每个URL配置Access-Control-Allow-Methods.
I don't want to configure Access-Control-Allow-Methods for each URL.
有没有一种方法可以根据我的路线来响应访问控制允许方法?
Is there a way to respond Access-Control-Allow-Methods based on my routes?
推荐答案
签出rack-cors gem,它可以像配置路由文件一样对其进行配置
check out the rack-cors gem, it allows you to configure it like a routes file
https://github.com/cyu/rack-cors
这篇关于如何在rails-api中响应OPTIONS HTTP方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!