You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by CGS <cg...@gmail.com> on 2012/08/30 18:05:14 UTC

Re: how to save json document into couchdb using javascript

[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 <le...@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.
>

Re: how to save json document into couchdb using javascript

Posted by Dave Cottlehuber <dc...@jsonified.com>.
On 3 September 2012 06:48, lenin <le...@gmail.com> wrote:
> yes...same ip for both.........
>
>
>
> --
> View this message in context: http://couchdb-development.1959287.n2.nabble.com/how-to-save-json-document-into-couchdb-using-javascript-tp7580825p7580843.html
> Sent from the CouchDB Development mailing list archive at Nabble.com.

Hi Lenin,

Let's shift this conversation to the user@ mailing list where it's
more appropriate.

You'll need to share some of your non-working code with us, to better
understand where you are stuck --  e.g. on http://friendpaste.com/ or
on gist.github.com.

The issue you are having is that the call you're trying to make is
forbidden because they're different origins. I suggest you google for
an explanation in your native language, it will help a lot.

There are many ways of working around this, but the simplest way in
CouchDB is simply to serve the HTML & the database from the same
origin. This is way cool. There is a tool called couchapp which
synchronises files on disk with design documents in your database and
makes this really really simple.

TL;DR:

- download & install couchapp for your platform[1]
- then `couchapp generate myapp`
- put your html files (like index.html and js files) in "myapp/_attachments/"
- `couchapp push` will send these new files to your localhost:5984 (in
admin party) into a new db called myapp
- visit the new url and play

You can use the same code that futon does jquery.couch.js[2] and check
in the test suite for examples on how to hook all of that together.
Running the test suite in the chrome developer tools gives a very good
overview of how this stuff should work.

Useful docs for jquery.couch.js[2] and test suite[3] follow.

A+
Dave

[1]: http://couchapp.org/
[2]: http://daleharvey.github.com/jquery.couch.js-docs/symbols/index.html
[3]: https://github.com/apache/couchdb/tree/1.2.0/share/www/script

Re: how to save json document into couchdb using javascript

Posted by lenin <le...@gmail.com>.
yes...same ip for both.........



--
View this message in context: http://couchdb-development.1959287.n2.nabble.com/how-to-save-json-document-into-couchdb-using-javascript-tp7580825p7580843.html
Sent from the CouchDB Development mailing list archive at Nabble.com.

Re: how to save json document into couchdb using javascript

Posted by CGS <cg...@gmail.com>.
Do you have the same IP and the same port for both requester and receiver?



On Fri, Aug 31, 2012 at 7:37 AM, lenin <le...@gmail.com> wrote:

> when i am running your code that i am getting the following error:
>
> *NS_ERROR_DOM_BAD_URI:Access to restricted URI denied*
>
> when i click the error n console it is going to the below line
>
> *xmlhttp.send("data="+JSON.stringify(myJSON));*
>
>
> please help to solve the above problem........
> thanks in advance
>
>
> Regards
> G.Lenin
> Research Scientist
>
>
>
> --
> View this message in context:
> http://couchdb-development.1959287.n2.nabble.com/how-to-save-json-document-into-couchdb-using-javascript-tp7580825p7580829.html
> Sent from the CouchDB Development mailing list archive at Nabble.com.
>

Re: how to save json document into couchdb using javascript

Posted by lenin <le...@gmail.com>.
when i am running your code that i am getting the following error:

*NS_ERROR_DOM_BAD_URI:Access to restricted URI denied*

when i click the error n console it is going to the below line

*xmlhttp.send("data="+JSON.stringify(myJSON));*


please help to solve the above problem........
thanks in advance


Regards
G.Lenin
Research Scientist



--
View this message in context: http://couchdb-development.1959287.n2.nabble.com/how-to-save-json-document-into-couchdb-using-javascript-tp7580825p7580829.html
Sent from the CouchDB Development mailing list archive at Nabble.com.

Re: can u please give me a full code

Posted by Noah Slater <ns...@tumbolia.org>.
You might have better luck on the user lists: http://couchdb.apache.org/

On Fri, Aug 31, 2012 at 6:17 AM, lenin <le...@gmail.com> wrote:

> what are the .js files has to import and
> can you please give me full code for connect couchdb using javascript
>
>
>
> --
> View this message in context:
> http://couchdb-development.1959287.n2.nabble.com/how-to-save-json-document-into-couchdb-using-javascript-tp7580825p7580828.html
> Sent from the CouchDB Development mailing list archive at Nabble.com.
>



-- 
NS

Re: can u please give me a full code

Posted by lenin <le...@gmail.com>.
what are the .js files has to import and 
can you please give me full code for connect couchdb using javascript 



--
View this message in context: http://couchdb-development.1959287.n2.nabble.com/how-to-save-json-document-into-couchdb-using-javascript-tp7580825p7580828.html
Sent from the CouchDB Development mailing list archive at Nabble.com.