简体   繁体   English

启用了 Elasticsearch https cors,但仍然在请求的资源上不存在“Access-Control-Allow-Origin”标头

[英]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.不是完美的正则表达式,但我的搜索应用程序现在可以工作了。

Those are not index specific settings.这些不是索引特定的设置。

The cors settings should be placed in the elasticsearch.yml config file of every node in your ES cluster and the cluster restarted.cors设置应放置在elasticsearch.yml每个节点的配置文件在您的ES集群和集群重启。 See here more about this.请参阅此处了解更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CORS所请求的资源上没有“ Access-Control-Allow-Origin”标头 - CORS No 'Access-Control-Allow-Origin' header is present on the requested resource 使用正确的标头和OPTIONS请求,仍会获得“所请求的资源上不存在Access-Control-Allow-Origin标头” - With correct headers and OPTIONS request, still getting “No Access-Control-Allow-Origin header is present on the requested resource” 带有Access-Control-Allow-Origin标头的Jquery + CORS +基本身份验证:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Jquery + CORS+ Basic Auth with Access-Control-Allow-Origin header: No 'Access-Control-Allow-Origin' header is present on the requested resource 尽管将 Cors 与 Axios 一起使用,我仍然收到此错误:请求的资源上不存在“Access-Control-Allow-Origin”标头 - Despite using Cors with Axios I still get this error: No 'Access-Control-Allow-Origin' header is present on the requested resource 尝试访问谷歌联系人api使用cors获取No“Access-Control-Allow-Origin”标头出现在请求的资源上 - trying to access google contacts api using cors getting No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 策略阻止了对获取的访问:请求的资源上不存在“Access-Control-Allow-Origin”header - Access to fetch has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 我收到错误消息:“...”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”header - I'm getting the error: "..." has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource localhost:4200 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头 - localhost:4200 has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource React - CORS 问题请求的资源上不存在“Access-Control-Allow-Origin”标头 - React - CORS issue No 'Access-Control-Allow-Origin' header is present on the requested resource 获取已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头 - fetch has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM