Pages

Showing posts with label couchdb. Show all posts
Showing posts with label couchdb. Show all posts

Tuesday, March 13, 2012

Ajax jQuery style


I am trying out the comma-first style for my objects. I got errors many times when I added a new pair to an object list because I didn't add a comma to the last pair.


Although curl gives a more informative message, I cannot get the same information out of statusCode 412.


<html>
<head>
<script type="text/javascript" src="jquerymin.js"></script>
<script type="text/javascript">
  function initialize() {
    $.ajax({
      type: "PUT"
    , url: "http://127.0.0.1/couchdb/albums"
    , success: function(data) {
        alert(data);
      }
    
    , statusCode: {
        412: function(jqXHR, textStatus, errorThrown) {
          alert(textStatus);
        }
      }
    });
  }
</script>
</head>
<body onload="initialize()">


</body>
</html>

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>