Pages

Tuesday, March 13, 2012

Simple CouchDB actions with Ajax

With rozky's most useful answer on how to make an Ajax script communicate with your CouchDB server,


  • 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);

I can execute some simple database commands as described in the Definitive Guide, replacing curl with some JavaScript code:

<html>
<head>
<script type="text/javascript">
  function initialize() {
    request=new XMLHttpRequest();
    request.open('GET','http://127.0.0.1/couchdb/_all_dbs',true);
    request.send(null);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        if (request.status == 200) {
          alert(request.responseText);
        }
      }
    };
  }
</script>
</head>
<body onload="initialize()">
</body>
</html>


No comments:

Post a Comment