incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From CGS <cgsmcml...@gmail.com>
Subject Re: how to save json document into couchdb using javascript
Date Thu, 30 Aug 2012 16:05:14 GMT
[forwarded to users as well]

Hi,

CouchDB is using JSON as document format, only that it needs to be
stringified to be compatible with AJAX POST method (at least this is the
recommendation from W3C). That means, using plain JavaScript, it should
look like this:

<script type="text/javascript">
> function saveCouchDBDoc(myJSON)
> var xmlhttp;
> var responseJSON;
> if (window.XMLHttpRequest)
>   {// code for IE7+, Firefox, Chrome, Opera, Safari
>   xmlhttp=new XMLHttpRequest();
>   }
> else
>   {// code for IE6, IE5
>   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
>   }
> xmlhttp.onreadystatechange=function()
>   {
>   if (xmlhttp.readyState==4 && xmlhttp.status==200)
>     {
>     responseJSON=JSON.parse(xmlhttp.responseText); // if you are sure it
> returns a JSON string
>     }
>   }
> xmlhttp.open("POST","/datadase_name",true);
>
> xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
> xmlhttp.send("data="+JSON.stringify(myJSON));
> }
> </script>

{
>
>
Just take care for x-origin permissions if you use another web server.

Another option would be using directly JSON by setting the header
"Content-Type" to "application/json" and data to myJSON.

I admit I never tried any of them directly with CouchDB web server because
I was using two-layer design (CouchDB + dedicated web server with reverse
proxy and/or middleware), so, the code is not 100% error free (e.g., if
"/database_name" doesn't work, I think you can use
"/_utils/database.html?database_name"). But, at least, it gives you a
starting point.

Don't forget about giving a name to the document and providing the revision
number if you already have a document.

CGS




On Thu, Aug 30, 2012 at 1:38 PM, lenin <leningovi@gmail.com> wrote:

> I am new in couchdb,
> please explain the steps to save json document into couchdb using javascipt
> thanks in advance,
>
>
>
> Regards
> G.Lenin
>
>
>
>
> --
> View this message in context:
> http://couchdb-development.1959287.n2.nabble.com/how-to-save-json-document-into-couchdb-using-javascript-tp7580825.html
> Sent from the CouchDB Development mailing list archive at Nabble.com.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message