views:

134

answers:

2

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.

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

duluthian
A: 

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)

rozky