[英]Elasticsearch https cors enabled but still getting No 'Access-Control-Allow-Origin' header is present on the requested resource
I have enabled the settings for cors in my index called myIndex, here are the settings for it.我在名为 myIndex 的索引中启用了 cors 的设置,这是它的设置。 But I when I try to pull the data from elasticsearch with some simple Javascript where the cors headers are set I get the error但是当我尝试使用一些简单的 Javascript 从 elasticsearch 中提取数据时,我得到了错误
XMLHttpRequest cannot load http://elasticSearchDomain.com:9200/myIndex/_search/ . XMLHttpRequest 无法加载http://elasticSearchDomain.com:9200/myIndex/_search/ 。 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' http://localhost:8000 ' is therefore not allowed access.因此,不允许访问 Origin ' http://localhost:8000 '。
I am running the Javascript by include it as a script that run when I open an html page through a python server.我通过将 Javascript 包含为脚本来运行 Javascript,该脚本在我通过 python 服务器打开 html 页面时运行。
{
"state": "open",
"settings": {
"index": {
"http": {
"cors": {
"allow-credentials": "true",
"enabled": "true",
"allow-origin": "*"
}
},
"creation_date": "1461087830891",
"number_of_shards": "5",
"number_of_replicas": "1",
"version": {
"created": "1070499"
},
"uuid": "2JeIgB7IRs6_DzEb6PLx-w"
}
},
"mappings": {
"tx": {
"properties": {
"next": {
"format": "dateOptionalTime",
"type": "date"
},
"eid": {
"index": "not_analyzed",
"type": "string"
},
"95percent_time": {
"type": "double"
},
"total_count": {
"type": "long"
},
"failure_count": {
"type": "long"
},
"pool_id": {
"type": "long"
},
"pool_name": {
"index": "not_analyzed",
"type": "string"
},
"failure_rate": {
"type": "double"
},
"report_time": {
"format": "dateOptionalTime",
"type": "date"
},
"txn_type": {
"index": "not_analyzed",
"type": "string"
},
"status_default": {
"type": "long"
},
"txn_name": {
"index": "not_analyzed",
"type": "string"
},
"frequency_type": {
"index": "not_analyzed",
"type": "string"
},
"status_2": {
"type": "long"
},
"status_1": {
"type": "long"
},
"avg_time": {
"type": "double"
},
"datacenter_id": {
"type": "long"
},
"datacenter_name": {
"index": "not_analyzed",
"type": "string"
},
"server_type": {
"type": "string"
}
}
}
},
"aliases": [
]
}
Here is my Javascript that Im trying to use to pull data from elasticsearch这是我试图用来从 elasticsearch 中提取数据的 Javascript
var url = "http://elasticSearchDomain.com:9200/myIndex/_search/";
var method = "POST";
var postData = '{"query": { "filtered": { "query": { "query_string": { "query": "*", "analyze_wildcard": true } }, "filter": { "bool": { "must": [ { "query": { "query_string": { "query": "*", "analyze_wildcard": true } } }, { "range": { "report_time": { "gte": 1458409392443, "lte": 1461001392443 } } } ], "must_not": [] } } } }, "size": 0, "aggs": { "2": { "date_histogram": { "field": "report_time", "interval": "12h", "pre_zone": "-07:00", "pre_zone_adjust_large_interval": true, "min_doc_count": 1, "extended_bounds": { "min": 1458409392443, "max": 1461001392443 } }, "aggs": { "3": { "terms": { "field": "pool_name", "size": 20, "order": { "_count": "desc" } } } } } } }';
// You REALLY want async = true.
// Otherwise, it'll block ALL execution waiting for server response.
var async = true;
var request = new XMLHttpRequest();
// specifies how the HTTP response will be handled.
request.onload = function () {
// You can get all kinds of information about the HTTP response.
var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
var data = request.responseText; // Returned data, e.g., an HTML document.
}
request.open(method, url, async);
request.setRequestHeader('Access-Control-Allow-Headers', '*');
request.setRequestHeader('Access-Control-Allow-Origin', '*');
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
// Actually sends the request to the server.
request.send(postData);
For some specificity (and for anyone having trouble with the regex part), I had to add these configurations in elasticsearch.yml:对于某些特殊性(以及对正则表达式部分有问题的任何人),我必须在 elasticsearch.yml 中添加这些配置:
http.cors.enabled: true
http.cors.allow-origin: /https?:\/\/(localhost)?(127.0.0.1)?(:[0-9]+)?/
Not perfect regex, but my search app works now.不是完美的正则表达式,但我的搜索应用程序现在可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.