CouchdB JQuery Ajax POST CORS

I am prototyping a very simple webapp. Today I installed Couch 1.3.1 and created a database. I am trying to save a document on my local couch (localhost: 5984) with POST from the client browser also on localhost, but a different port (6789)

var dbIp = "http://localhost:5984/commute"; var data = {state:0,timestamp:"faketime"}; $.ajax({ type: 'POST', crossDomain: true, contentType: "application/json", url: dbIp, data: data, success: function(result) { console.log(result); } }); 

I get:

 XMLHttpRequest cannot load http://localhost:5984/commute-tracker. Origin http://localhost:6789 is not allowed by Access-Control-Allow-Origin. 

I changed local.ini to enable CORS as described in couchdb spec with

 [httpd] enable_cors = true [cors] origins = * [cors] methods = GET, POST, PUT, DELETE 

I see all these changes reflected in the configuration file in futon. I also checked the swirl database:

 curl -X POST localhost:5984/commute -H "Content-Type: application/json" -d '{"tags":"sure","name":"made it"}' 

Twisting works just fine, but I can't do a similar POST in the browser due to Allow Origin access control. What else am I missing, or what can I change to make this POST possible?

+4
source share
1 answer

Trying to make a cross-domain, is this CORS or JSONP perfect.

Since you actually control the web server instance and the couchdb instance (I assume you have administrator rights on the server), I would recommend using a reverse proxy to serve couchdb as the endpoint for the main web application.

If you use Apache mod_proxy , you can do it, or nginx is another great HTTP HTTP proxy server ... pasting the config directive, for example:

ProxyPass /couch_db/ http://www.localhost:5984/ (for mod_proxy)

will allow you to send a message http://localhost:6789/couch_db/commute

0
source

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


All Articles