【问题标题】:XMLHttpRequest querying couchdb: CORS headers set, but does not workXMLHttpRequest 查询 couchdb:设置了 CORS 标头,但不起作用
【发布时间】:2016-10-14 05:20:27
【问题描述】:

我有一个 apache 并运行一个简单的 html 页面,其中包括 java 脚本,用于从运行在同一台机器(但不同端口)上的 couchdb 服务器查询数据。

<script>
        updateHistory();

        function updateHistory() {
            var Httpreq = new XMLHttpRequest();

            Httpreq.open("GET", "http://192.168.178.43:5984/temp_data/_design/dataView/_view/getAll", false);
            Httpreq.send(null);

            console.log(Httpreq.responseText);
            var dataObj = JSON.parse(Httpreq.responseText);
            document.getElementById('data-id').innerHTML = Httpreq.responseText;
        }
</script>

我不断收到 cors header 'access-control-allow-origin' missing 错误。所以我在我的 apache 配置中添加了 CORS 标头。

    # Always set these headers.
    # origin must match exactly the URL in browser address bar
    Header set Access-Control-Allow-Origin "*"

    # set all headers needed, wildcard does not work!
    Header set Access-Control-Allow-Headers "Accept,Accept-Charset,Accept-Encoding,Accept-Language,Connection,Content-Type,Cookie,DNT,Host,Keep-Alive,Origin,Referer,User-Agent,X-CSRF-Token,X-Requested-With"

    # set allowed methods (GET,PUT,POST,OPTIONS,..) or * for all
    Header set Access-Control-Allow-Methods "*"

    # allow credentials (cookies)
    Header set Access-Control-Allow-Credentials true

我在 firefox 中验证...标题设置正确,但我不断收到错误消息。现在我看不出我做错了什么。

这里是请求头:

Host: 192.168.178.43
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 
Firefox/46.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://192.168.178.43/index.html
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

然后响应...

Accept-Ranges: bytes
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 497
Content-Type: text/html
Date: Mon, 13 Jun 2016 17:11:06 GMT
Etag: "352-5352bf8ecde14-gzip"
Keep-Alive: timeout=5, max=100
Last-Modified: Mon, 13 Jun 2016 17:10:59 GMT
Server: Apache/2.4.10 (Raspbian)
Vary: Accept-Encoding
access-control-allow-credentials: true
access-control-allow-headers: Accept,Accept-Charset,Accept-Encoding,Accept-Language,Connection,Content-Type,Cookie,DNT,Host,Keep-Alive,Origin,Referer,User-Agent,X-CSRF-Token,X-Requested-With

提前致谢

【问题讨论】:

    标签: javascript apache xmlhttprequest cors couchdb


    【解决方案1】:

    好的,想通了。我必须在 local.ini 中的 couchdb 服务器上启用 CORS:

    [httpd]
    enable_cors = true
    
    [cors]
    origins = http://192.168.178.43
    

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 2015-09-01
      • 2017-03-23
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2016-04-09
      • 2013-09-18
      • 2014-10-07
      相关资源
      最近更新 更多