How to POST new data in CouchDB using JavaScript / jQuery

I found the answer here to the question below.

I needed to configure a reverse proxy in apache, which took about 2 minutes, adding the following line to my virual host;
ProxyPass / couchdb / http: // dojo: 5984 /
Due to the same origin policy, you cannot send data through ports. I knew that this applies to domains, but not to other ports, so you are setting up a reverse proxy.


I would like to know how I can send POST data to couchDB using JavaScript or jQuery.

I followed this tut and created a database, and I can send and receive data using curl, and it all works fine. The following are examples of twisting that I used.

I can also get data using jQuery, but I don't know how to use POST for CouchDB

curl -X GET http://127.0.0.1:5984/mycouchshop/_all_docs .
curl -X POST http://127.0.0.1:5984/mycouchshop/ -d @ person.json -H "Content-Type: application / json"

I can receive and display data using jQuery. The code below works fine.

$.ajax({ url : 'http://couchdb:5984/mycouchshop/_design/peoples/_view/people', type : 'GET', dataType : "jsonp", success : function(json) {} }); 

But publishing the data leads to an invalid 405 method

  $.ajax({ url : 'http://couchdb:5984/mycouchshop/', data : {"forename": "Bob", "surname": "McHamster", "type": "person"}, contentType : "application/json", type : 'POST', dataType : "json", success : function(resp) {} }); 
+6
source share
2 answers

Security Policy of Same Origin

I am not a couchapp expert, but I ran into the same problem. The problem is that you fall into cross-domain restrictions, your application is served from one port, and on couchdb, to another port. From couchapp.org :

The usual question I get from people starting to write Ajax applications using CouchDB is "when I try to query CouchDB using jQuery, it doesn't work." It usually turns out that they have an index.html file on their file system, which is trying to make an Ajax call against the CouchDB server. After I explain to them the same security policy of origin, they begin to understand this means that CouchDB must serve its HTML code (instead of loading it into the browser directly from the file system).

So, the simplest CouchApp is just an HTML file served directly from CouchDB that uses Ajax to load and save data from CouchDB.

Using couchapp

It seems that all application files should be β€œpushed” to the couchdb server using couchapp (http://couchapp.org/page/index). I work with a Mac, so I used a standalone executable . Couchapp installation instructions there

Textbook

When you understand how couchapp works, you can use this tutorial

Next steps

I try to understand them ... if you find anything good, please share! Good luck

EDIT: I just found this tutorial

+3
source

Another solution to solving the Cross-Origin Resource Sharing ( CORS ) problem is to change some of the settings for your local CouchDB installation.

Just follow the answers posted on this question: Problems with Couchdb cors

+1
source

Source: https://habr.com/ru/post/893675/


All Articles