来自angularJS的已启用请求标头的本地elasticsearch主机阻止请求

时间:2016-05-11 09:58:56

标签: angularjs elasticsearch elasticsearch-query

我正在尝试向本地弹性搜索发布请求。 我已将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 ]
                }
              }
            }
          }

        }
      }
    }
  };

------- ANSWER ---------

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

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

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

0 个答案:

没有答案