javascript 如何在 couchDB 中添加 cors -- 请求的资源上不存在“Access-Control-Allow-Origin”标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20897033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add cors in couchDB -- No 'Access-Control-Allow-Origin' header is present on the requested resource
提问by ess
I am trying to create a html file which synchronize data from a pouchDb to couchDb ..but iam getting the following error in chrome console.
我正在尝试创建一个 html 文件,该文件将数据从 pouchDb 同步到 couchDb ..但是我在 chrome 控制台中收到以下错误。
Uncaught TypeError: Cannot call method 'addEventListener' of null
OPTIONS http://localhost:5984/todos/405 (Method Not Allowed)
OPTIONS http://localhost:5984/todos/No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. XMLHttpRequest cannot load http://localhost:5984/todos/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. OPTIONS http://localhost:5984/todos/No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. pouchdb-nightly.js:3496 OPTIONS http://localhost:5984/todos/No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. pouchdb-nightly.js:3496 XMLHttpRequest cannot load http://localhost:5984/todos/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
未捕获的类型错误:无法调用 null 的方法“addEventListener”
选项http://localhost:5984/todos/405(不允许的方法)
选项http://localhost:5984/todos/请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8080'。XMLHttpRequest 无法加载http://localhost:5984/todos/。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8080'。选项http://localhost:5984/todos/请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8080'。pouchdb-nightly.js:3496 选项http://localhost:5984/todos/请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8080'。pouchdb-nightly.js:3496 XMLHttpRequest 无法加载http://localhost:5984/todos/。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8080'。
I found a way to avoid this issue. That is i have to open chrome using the below command: cd C:\Program Files (x86)\Google\Chrome\Application Chrome.exe --disable-web-security.But this didn't solve the issue. I saw a lot of similar question here.but i didn't understand any answer since i am relatively new in this field
我找到了避免这个问题的方法。那就是我必须使用以下命令打开 chrome:cd C:\Program Files (x86)\Google\Chrome\Application Chrome.exe --disable-web-security。但这并没有解决问题。我在这里看到了很多类似的问题。但我不明白任何答案,因为我在这个领域相对较新
回答by robertc
From this documentation:
从这个文档:
To enable CORS support, you need to set the
enable_cors = true
option in the[httpd]
section oflocal.ini
, and add a[cors]
section containing aorigins = *
setting. Note that by default, no origins are accepted; you must either use a wildcard or whitelist.[httpd] enable_cors = true [cors] origins = *
要启用 CORS 支持,您需要
enable_cors = true
在[httpd]
部分设置选项local.ini
,并添加一个[cors]
包含origins = *
设置的部分。请注意,默认情况下,不接受任何来源;您必须使用通配符或白名单。[httpd] enable_cors = true [cors] origins = *
回答by HADI
Accepted answer doesn't solve my problem. What i did is -
接受的答案并不能解决我的问题。我所做的是——
update local.ini (/installation location/CouchDB/etc/couchdb)
更新 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 -
然后以管理员模式打开CMD并运行-
net.exe stop "Apache CouchDB" && net.exe start "Apache CouchDB"
*** for windows user only
*** 仅适用于 Windows 用户
回答by nlawson
There is now a tool that will add CORS to CouchDB for you:
现在有一个工具可以为您将 CORS 添加到 CouchDB:
npm install -g add-cors-to-couchdb
add-cors-to-couchdb
More documentation here: https://github.com/pouchdb/add-cors-to-couchdb
回答by Rodrigo Rubio
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”,如下所示:
add-cors-to-couchdb <you_url>:<port> -u <user_name> -p <your_password>
Check out the following link for CouchDB how to CORS reference
回答by Robert Sasak
CORS section in couchdb:
couchdb 中的 CORS 部分:
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
注意 x-requested-with
回答by Marc McElhinney
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
对于 Windows 用户,进入 apache 安装文件夹(C:\Program Files (x86)\Apache Software Foundation\CouchDB\etc\couchdb)<--为我在这里,并使用以下详细信息更新 local.ini 文件.. . 对我很有用,谢谢大家
[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 :)
显然,为了完整起见,重新启动 apache couchdb 服务:)
回答by Michael
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.
根据@szydan 评论,如果您的包管理器仍然为您提供不支持 CORS 选项的 1.2 版本,您需要从源代码安装它。
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
您可能需要安装 SpiderMonkey 和 jsapi 依赖项
回答by Gabriel Mancini
Must put OPTIONS
on methods attribute.
必须加上OPTIONS
methods属性。
[cors]
credentials: true
headers: accept, authorization, content-type, origin, referer, cache-control, x-requested-with
methods: GET,PUT,POST,HEAD,DELETE,OPTIONS
origins: *
[httpd]
enable_cors = true
even if u bootstrap your db on-the-fly (nodejs/nano version):
即使您即时引导您的数据库(nodejs/nano 版本):
nano.request({db: '_node', path: '/nonode@nohost/_config/couch_peruser/enable', method: 'PUT', body: 'true'})
.then(() => nano.request({db: '_node', path: '/nonode@nohost/_config/httpd/enable_cors', method: 'PUT', body: 'true'}))
.then(() => nano.request({db: '_node', path: '/nonode@nohost/_config/cors/origins', method: 'PUT', body: '*'}))
.then(() => nano.request({db: '_node', path: '/nonode@nohost/_config/cors/credentials', method: 'PUT', body: 'true'}))
.then(() => nano.request({db: '_node', path: '/nonode@nohost/_config/cors/headers', method: 'PUT', body: 'accept, authorization, content-type, origin, referer'}))
.then(() => nano.request({db: '_node', path: '/nonode@nohost/_config/cors/methods', method: 'PUT', body: 'GET, PUT, POST, HEAD, DELETE, OPTIONS'}))