how to add cors in couchDB — No 'Access-Control-Allow-Origin' header is present on the requested resource

こ雲淡風輕ζ 提交于 2019-11-30 06:25:39

From this documentation:

To enable CORS support, you need to set the enable_cors = true option in the [httpd] section of local.ini, and add a [cors] section containing a origins = * setting. Note that by default, no origins are accepted; you must either use a wildcard or whitelist.

[httpd]
enable_cors = true

[cors]
origins = *

Accepted answer doesn't solve my problem. What i did is -

update local.ini (/installation location/CouchDB/etc/couchdb)

[httpd]
enable_cors = true

[cors]
origins = *
credentials = true
methods = GET, PUT, POST, HEAD, DELETE
headers = accept, authorization, content-type, origin, referer, x-csrf-token

Then open CMD in administrator mode and run -

net.exe stop "Apache CouchDB" && net.exe start "Apache CouchDB"

*** for windows user only

CORS section in couchdb:

credentials: true
headers: accept, authorization, content-type, origin, referer, cache-control, x-requested-with
methods: GET,PUT,POST,HEAD,DELETE
origins: *

Notice x-requested-with

There is now a tool that will add CORS to CouchDB for you:

npm install -g add-cors-to-couchdb
add-cors-to-couchdb

More documentation here: https://github.com/pouchdb/add-cors-to-couchdb

Easiest way i found is to install the following:

npm install -g add-cors-to-couchdb

Then run the tool i.e. "add-cors-to-couchdb" from the command line like this:

add-cors-to-couchdb <you_url>:<port> -u <user_name> -p <your_password>

Check out the following link for CouchDB how to CORS reference

Per @szydan comment, in case your package manager still serves you 1.2 version, which doesn't support CORS OPTIONS, you need to install it from source.

echo "deb http://binaries.erlang-solutions.com/debian `lsb_release -cs` contrib"  | tee /etc/apt/sources.list.d/erlang-solutions.list
wget -O - http://binaries.erlang-solutions.com/debian/erlang_solutions.asc |  apt-key add -
echo "deb http://packages.cloudant.com/debian `lsb_release -cs` main" | tee /etc/apt/sources.list.d/cloudant.list
wget http://packages.cloudant.com/KEYS -O - |  apt-key add -
apt-get update -y
apt-get install -y erlang-nox erlang-dev
apt-get install -y build-essential
apt-get install -y erlang-nox
apt-get install -y libmozjs185-cloudant libmozjs185-cloudant-dev
apt-get install -y libnspr4 libnspr4-0d libnspr4-dev libcurl4-openssl-dev curl libicu-dev

useradd -d /var/lib/couchdb couchdb
mkdir -p /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb /var/lib/couchdb
chown -R couchdb:couchdb /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb
chmod -R g+rw /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb


http://www.apache.org/dyn/closer.cgi?path=/couchdb/source/

tar xzf apache-couchdb-*.tar.gz
cd apache-couchdb-*
./configure --prefix=/usr/local --with-js-lib=/usr/lib --with-js-include=/usr/include/mozjs --enable-init
make && sudo make install

sudo ln -s /usr/local/etc/init.d/couchdb /etc/init.d/couchdb
sudo /etc/init.d/couchdb start
sudo update-rc.d couchdb defaults

#check if it runs
curl http://127.0.0.1:5984/

You may need to install SpiderMonkey and jsapi dependencies

For windows users , go into apache install folder(C:\Program Files (x86)\Apache Software Foundation\CouchDB\etc\couchdb) <-- was here for me , and update the local.ini file with the below details ...worked a charm for me , thanks guys

 [cors]
 credentials: true
 headers: accept, authorization, content-type, origin, referer, cache-control,          x-requested-with
 methods: GET,PUT,POST,HEAD,DELETE
 origins: *

 [httpd]
 enable_cors = true

obviously , restart the apache couchdb service for completeness :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!