XMLHttpRequest查询couchdb:CORS标头设置,但不起作用

时间:2016-06-13 17:22:50

标签: javascript apache xmlhttprequest cors couchdb

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

include uwsgi_params;

我一直在 cors标题'access-control-allow-origin'丢失错误。所以我在apache配置中添加了CORS Headers。

<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>

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

这里是请求标题:

    # 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

响应......

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

提前致谢

1 个答案:

答案 0 :(得分:0)

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

[httpd]
enable_cors = true

[cors]
origins = http://192.168.178.43