CouchDB从XMLHttpRequest跨域访问?

发布于 2024-09-12 07:29:06 字数 315 浏览 4 评论 0原文

目前,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清秋悲枫 2024-09-19 07:29:06

我发现解决这个问题的最简单方法是使用本地安装的 Apache Web Server 并启用 mod_proxy 模块并配置 ProxyPass 指令。

让我们从基本设置开始

index.html 有以下内容

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

var http = XMLHttpRequest();
http.open('GET', 'http://127.0.0.1:5984/_all_dbs', true); // ! WE WILL CHANGE THIS LINE
http.onreadystatechange = function() {
    if (http.readyState == 4 && http.status == 200) {
        console.debug('it works');
    }
};
http.send(null)
</script>
<head><title>Test Access to CouchDB</title></head>
<body>
</body>
</html>

如果您刚才尝试,由于跨域问题,它将无法工作(在本例中,端口不匹配 8181 != 5984)。

如何修复它

  • 配置 Apache (apache_home/conf/httpd.conf)
    • 取消注释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)
    • 重新启动 Apache
  • 修改 index.html
    • 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

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

var http = XMLHttpRequest();
http.open('GET', 'http://127.0.0.1:5984/_all_dbs', true); // ! WE WILL CHANGE THIS LINE
http.onreadystatechange = function() {
    if (http.readyState == 4 && http.status == 200) {
        console.debug('it works');
    }
};
http.send(null)
</script>
<head><title>Test Access to CouchDB</title></head>
<body>
</body>
</html>

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

  • configure Apache (apache_home/conf/httpd.conf)
    • uncomment LoadModule proxy_module modules/mod_proxy.so
    • uncomment LoadModule proxy_http_module modules/mod_proxy_http.so
    • add ProxyPass /couchdb http://127.0.0.1:5984 (as top level property like ServerAdmin)
    • restart Apache
  • modify index.html
    • replace http.open('GET', 'http://127.0.0.1:5984/_all_dbs', true); with http.open('GET', '/couchdb/_all_dbs', true);

Try now and you should see 'it works' output in the javascript console (I used Firebug Console)

浊酒尽余欢 2024-09-19 07:29:06

您可以使用 CouchDB show 函数来设置 Access-Control-Allow-Origin 标头。


function(doc, req) {
  return {
    body : 'whatever',
    headers : {
      "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.


function(doc, req) {
  return {
    body : 'whatever',
    headers : {
      "Access-Control-Allow-Origin": "\"*\""
    }
  }
}

More info on show functions here: http://guide.couchdb.org/draft/show.html

你怎么这么可爱啊 2024-09-19 07:29:06

CouchDB 1.3 通过 CORS 解决了这个问题: https://wiki.apache.org/couchdb/CORS

CouchDB 1.3 solves this with CORS: https://wiki.apache.org/couchdb/CORS

£噩梦荏苒 2024-09-19 07:29:06

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.

白龙吟 2024-09-19 07:29:06

您应该启用 CouchDB 中的 CORS > 1.3.这就像编辑 default.ini 并设置 enable_cors = true 然后修改 [cors] 下的 origins 一样简单code> 部分包含您需要的顶级 url。例如,我必须执行以下操作才能将本地 grunt 服务器列入白名单。

enable_cors = true
[cors]
origins = http://127.0.0.1:9000

要完全回答这个问题,尽管您想要设置,

origins = *

但这可能被认为是一个漏洞,并且您可能应该更多地限制起源。

you should enable CORS in CouchDB > 1.3. This is as simple as editing your default.ini and setting enable_cors = true and then modifying origins 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.

enable_cors = true
[cors]
origins = http://127.0.0.1:9000

to fully answer this question though you'd want to set

origins = *

though this could be argued to be a vulnerability, and you should probably restrict the origins more.

欢你一世 2024-09-19 07:29:06

我解决这个问题的方法是编写一个 2 行 Rebol 包装器 CGI,然后要求 Jquery 中的 Ajax 调用我的 CGI,而不是从 Couchdb 获取数据的 URL:

Rebol [

  {wrapper to overcome cross-domain fetching of data from couchdb}
 ]
print "Content-type: application/json^/"  ;text/plain^/"
print read http://127.0.0.1:5984/syncspace/_design/vals/_view/xxxx?group=true

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:

Rebol [

  {wrapper to overcome cross-domain fetching of data from couchdb}
 ]
print "Content-type: application/json^/"  ;text/plain^/"
print read http://127.0.0.1:5984/syncspace/_design/vals/_view/xxxx?group=true
今天小雨转甜 2024-09-19 07:29:06

我制作了一个返回 JSONp 的列表...但仍然只读支持

"jsonp": "function(head, req) {
    var row;
    var rows=[];
    while(row = getRow()){
        rows.push(row);
    }
    rj = JSON.stringify({\"rows\" : rows,\"total_rows\":rows.length});
    return req.query.callback+\"(\"+rj+\");\";
}",

I made a list that returns JSONp...but still read only support

"jsonp": "function(head, req) {
    var row;
    var rows=[];
    while(row = getRow()){
        rows.push(row);
    }
    rj = JSON.stringify({\"rows\" : rows,\"total_rows\":rows.length});
    return req.query.callback+\"(\"+rj+\");\";
}",
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文