【发布时间】: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