CouchDB从XMLHttpRequest跨域访问?
目前,Web 应用程序需要提供某种跨域 HTTP 标头来访问其他域上的数据: http:// /openfontlibrary.org/wiki/Web_Font_linking_and_Cross-Origin_Resource_Sharing
有没有办法配置CouchDB支持无限制的跨域访问? (它可能在内部使用 apache httpd)我仅使用 db 内部用途。
Currently, web application need to offer some kind of cross-domain HTTP header to access data on other domain: http://openfontlibrary.org/wiki/Web_Font_linking_and_Cross-Origin_Resource_Sharing
Is there any way to configure CouchDB to support unlimited cross-domain access? (it may use apache httpd internally) I'm using the db in-house purpose only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我发现解决这个问题的最简单方法是使用本地安装的 Apache Web Server 并启用 mod_proxy 模块并配置 ProxyPass 指令。
让我们从基本设置开始
index.html 有以下内容
如果您刚才尝试,由于跨域问题,它将无法工作(在本例中,端口不匹配 8181 != 5984)。
如何修复它
LoadModule proxy_module module/mod_proxy.so
LoadModule proxy_http_module module/mod_proxy_http.so
ProxyPass /couchdb http://127.0.0.1:5984
(作为顶级属性,如 ServerAdmin)http.open('GET', 'http://127.0.0.1:5984/_all_dbs', true); 替换为
http.open('GET' , '/couchdb/_all_dbs', true);
立即尝试,您应该在 JavaScript 控制台中看到“it Works”输出(我使用了 Firebug 控制台)
The easiest way I found to solve it is by using locally installed Apache Web Server with enabled mod_proxy module and configured ProxyPass directive.
Let start with basic setup
index.html has the following content
If you try it just now it will not work because of the cross domain problem (in this instance ports don't match 8181 != 5984).
How to fix it
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
ProxyPass /couchdb http://127.0.0.1:5984
(as top level property like ServerAdmin)http.open('GET', 'http://127.0.0.1:5984/_all_dbs', true);
withhttp.open('GET', '/couchdb/_all_dbs', true);
Try now and you should see 'it works' output in the javascript console (I used Firebug Console)
您可以使用 CouchDB show 函数来设置 Access-Control-Allow-Origin 标头。
有关显示功能的更多信息,请访问:http://guide.couchdb.org/draft/show.html< /a>
You could use a CouchDB show function to set the Access-Control-Allow-Origin header.
More info on show functions here: http://guide.couchdb.org/draft/show.html
CouchDB 1.3 通过 CORS 解决了这个问题: https://wiki.apache.org/couchdb/CORS
CouchDB 1.3 solves this with CORS: https://wiki.apache.org/couchdb/CORS
Eonil,我也想要跨域访问,但 CouchDB 不支持,
您可以在这里投票支持要实现的功能:
https://issues.apache.org/jira/browse/COUCHDB-431
ps:该功能请求已于 2009 年 7 月 23 日创建:(我希望他们能听到我们的声音。
Eonil, I want Cross-Domain access too, but is not supported by CouchDB,
you can vote for that feature to be implemented here:
https://issues.apache.org/jira/browse/COUCHDB-431
ps: that feature request has been created on 23/Jul/09 :( I hope they hear us.
您应该启用 CouchDB 中的 CORS > 1.3.这就像编辑
default.ini
并设置enable_cors = true
然后修改[cors]
下的origins
一样简单code> 部分包含您需要的顶级 url。例如,我必须执行以下操作才能将本地 grunt 服务器列入白名单。要完全回答这个问题,尽管您想要设置,
但这可能被认为是一个漏洞,并且您可能应该更多地限制起源。
you should enable CORS in CouchDB > 1.3. This is as simple as editing your
default.ini
and settingenable_cors = true
and then modifyingorigins
under the[cors]
section to have the top level urls you need. For example I had to do the following to whitelist my local grunt server.to fully answer this question though you'd want to set
though this could be argued to be a vulnerability, and you should probably restrict the origins more.
我解决这个问题的方法是编写一个 2 行 Rebol 包装器 CGI,然后要求 Jquery 中的 Ajax 调用我的 CGI,而不是从 Couchdb 获取数据的 URL:
The way I have solved this is to write a 2 line Rebol wrapper CGI and then ask my Ajax in Jquery to call my CGI instead of the URL for getting data from Couchdb:
我制作了一个返回 JSONp 的列表...但仍然只读支持
I made a list that returns JSONp...but still read only support