【问题标题】:how to add cors in couchDB -- No 'Access-Control-Allow-Origin' header is present on the requested resource如何在 couchDB 中添加 cors - 请求的资源上不存在“Access-Control-Allow-Origin”标头
【发布时间】:2014-01-20 17:34:36
【问题描述】:

我正在尝试创建一个 html 文件,它将数据从 pouchDb 同步到 couchDb ..但我在 chrome 控制台中收到以下错误。

未捕获的类型错误:无法调用 null 的方法“addEventListener”

选项http://localhost:5984/todos/ 405(方法不是 允许)

OPTIONS http://localhost:5984/todos/ 上没有“Access-Control-Allow-Origin”标头 请求的资源。因此,Origin 'http://localhost:8080' 不允许访问。 XMLHttpRequest 无法加载 http://localhost:5984/todos/。没有“访问控制允许来源” 请求的资源上存在标头。原点'http://localhost:8080' 因此不允许访问。 选项http://localhost:5984/todos/ 没有“访问控制允许来源”标头 存在于请求的资源上。原点'http://localhost:8080' 因此不允许访问。 pouchdb-nightly.js:3496 选项http://localhost:5984/todos/ 不存在“Access-Control-Allow-Origin”标头 在请求的资源上。原点'http://localhost:8080' 因此不允许访问。 pouchdb-nightly.js:3496 XMLHttpRequest 无法加载 http://localhost:5984/todos/。不 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:8080” 访问。

我找到了避免此问题的方法。那就是我必须使用以下命令打开 chrome:cd C:\Program Files (x86)\Google\Chrome\Application Chrome.exe --disable-web-security。但这并没有解决问题。 我在这里看到了很多类似的问题。但我不明白任何答案,因为我在这个领域相对较新

【问题讨论】:

  • 您遇到了我们所说的"same origin policy"。默认情况下,浏览器不允许您向与托管页面的服务器不具有相同协议和端口的服务器发出 ajax 请求。链接文章中有很多很好的信息。
  • 谢谢.....但是该链接没有包含标题的任何代码
  • 您无法在客户端 JavaScript 或 html 文件中设置该标头。它正在谈论来自服务器的响应中的标头。
  • 你能给出一个解决方案吗....我正在使用html,js和同步pouchdb和couchdb pouchdb.com/getting-started.html
  • 可以通过 response.addheader("","*"); 设置header在客户端

标签: javascript html cors couchdb pouchdb


【解决方案1】:

来自this documentation

要启用 CORS 支持,您需要设置 enable_cors = true 选项 在local.ini[httpd] 部分中,并添加一个[cors] 部分 包含origins = * 设置。请注意,默认情况下,没有来源 公认;您必须使用通配符或白名单。

[httpd]
enable_cors = true

[cors]
origins = *

【讨论】:

  • 谢谢@robertc ...你能解释一下“通配符还是白名单”...我是这个领域的新手
  • 我在 local.ini 中添加了 cors...但出现以下错误...-> 无法加载资源:凭据标志为“真”,但“访问控制允许-凭据的标题是 ''。
  • 我明白了.........谢谢@robertc..我在 local.ini 中添加了以下行...[cors] origins = * credentials = true methods = GET, PUT,POST,HEAD,DELETE headers= 接受、授权、内容类型、来源
  • 所有通过包管理器在 debian/ubuntu 上安装 couchdb 的注意事项。截至今天(2014 年 10 月 10 日),您将获得的版本是 1.2,它不支持 CORS 选项。如果你想使用它们,你必须从源代码安装 never 版本。我花了一点时间才意识到为什么我的 CORS 设置不起作用,原因是我通过 apt-get install couchdb 安装了它,它是 1.2 版。
  • @szydan 在 Ubuntu 上,您可以使用以下 PPA 安装支持 CORS 的 1.6.1 版。 launchpad.net/~couchdb/+archive/ubuntu/stable
【解决方案2】:

接受的答案并不能解决我的问题。我所做的是 -

更新local.ini(/安装位置/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

然后以管理员模式打开CMD并运行——

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

*** 仅适用于 windows 用户

【讨论】:

【解决方案3】:

couchdb 中的 CORS 部分:

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

通知x-requested-with

【讨论】:

    【解决方案4】:

    现在有一个工具可以为您将 CORS 添加到 CouchDB:

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

    更多文档在这里:https://github.com/pouchdb/add-cors-to-couchdb

    【讨论】:

      【解决方案5】:

      我发现最简单的方法是安装以下内容:

      npm install -g add-cors-to-couchdb
      

      然后像这样从命令行运行工具,即“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

      【讨论】:

        【解决方案6】:

        根据@szydan 的评论,如果你的包管理器仍然为你提供不支持 CORS OPTIONS 的 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/
        

        您可能需要安装 SpiderMonkey 和 jsapi 依赖项

        【讨论】:

          【解决方案7】:

          对于 windows 用户,进入 apache 安装文件夹(C:\Program Files (x86)\Apache Software Foundation\CouchDB\etc\couchdb)

           [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
          

          显然,为了完整性,重启 apache couchdb 服务:)

          【讨论】:

            【解决方案8】:

            必须将OPTIONS 放在方法属性上。

            [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
            

            即使你在运行时引导你的数据库(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'}))
            

            【讨论】:

              【解决方案9】:

              可以通过http://localhost:5984/_utils(或运行 CouchDB 的任何 URL,后跟 /_utils)访问 CouchDB UI。在其中,您可以转到 Configuration > CORS 并启用或禁用 CORS。

              【讨论】:

                猜你喜欢
                • 2014-07-06
                • 2017-04-11
                • 2022-07-28
                • 1970-01-01
                • 2020-02-13
                • 2017-12-16
                • 2017-08-19
                • 2021-07-17
                相关资源
                最近更新 更多