0

我正在尝试向本地弹性搜索发布请求。我已将 elasticsearch.yml 配置为包括:

  http.cors.enabled: true
  http.cors.allow-origin: "*"
  http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"

我也试过:

  http.cors.enabled: true
  http.cors.allow-origin: "*"
  http.cors.allow-headers: X-Requested-With, Content-Type, Content-Length, Authorization

从 Postman 发布时没有问题,但是从 AngularJS 发布时,我收到此错误:

XMLHttpRequest cannot load http://localhost:9200/_search/test_index/_search?size=50. Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

角码如下:

$http.post( vm.elasticsearchUrl, query );

var query = {
    index: 'test_index',
    size: 50,
    body: {
      "query": {
        "filtered": {
          "query": {
            "wildcard": {
              "title": "*" + searchTerm + "*"
            }
          },
          "filter": {
            "not": {
              "filter": {
                "terms": {
                  "id": [ 1, 12 ]
                }
              }
            }
          }

        }
      }
    }
  };

- - - -回答 - - - - -

我通过将 Content-Type 设置为 undefined 来解决它,然后从请求标头中删除 Content-Type。

var promise = $http.post( vm.elasticsearchUrl, query, {
    headers: {
      'Content-Type': undefined
    }
  } );

我不确定这是否是正确的做法,所以如果有人有任何批评,我很想听听。

4

0 回答 0