【问题标题】:Using angularjs to request elasticsearch as cross domain使用angularjs请求elasticsearch作为跨域
【发布时间】:2016-06-29 23:00:31
【问题描述】:

我正在使用 angularjs 制作一个网络应用程序。我正在对 elasticsearch api 提出请求。这是代码:

app.controller('usersDatas', function($scope, $http){
  $http.post("http://192.168.6.55:9200/userdata/_search", {headers : { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
    data: "{ 'query': { 'query_string': { 'query': 'png' } } }"
    // Access-Control-Allow-Origin: true
  }).success(function(data){
    console.log(data);
  });
});

这是我遇到的错误:

XMLHttpRequest cannot load http://192.168.6.55:9200/userdata/_search. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

我已经添加了这个:

http.cors:
  enable: true
  allow-origin: /https?:/\/\/localhost(:[0-9]+)?/

到我的弹性搜索 (/etc/elasticsearch/elasticesarch.yml) 配置。我不知道问题是来自elasticsearch还是来自angularjs。 我该如何解决这个问题? 谢谢。

【问题讨论】:

  • 你用的是windows还是linux系统还是ios??
  • 我使用的是 ubuntu 16.04

标签: angularjs elasticsearch cross-domain


【解决方案1】:

必须在 HEADER Access-Control-Allow-Origin 中添加

PHP 示例:

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

【讨论】:

    【解决方案2】:

    这是浏览器问题首先关闭所有chrome浏览器窗口...

    • 打开终端

    • sudo -i

    • (您的密码)

    然后输入

    • google-chrome --disable-web--security

    然后尝试该网址中的链接

    【讨论】:

      猜你喜欢
      • 2016-03-15
      • 2015-01-26
      • 2014-04-28
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多