2016-06-29 28 views
0

我正在使用angularjs製作一個web應用程序。我正在對elasticsearch API進行請求。這是代碼:使用angularjs請求彈性搜索作爲跨域

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]+)?/ 

我elasticsearch(/etc/elasticsearch/elasticesarch.yml)配置。我不知道問題是來自elasticsearch還是來自angularjs。 我該如何解決這個問題? 謝謝。

+0

ü使用基於Windows或Linux的系統或ios? – Gopinath

+0

我使用的是Ubuntu 16.04 – theCode

回答

0

這是瀏覽器的問題首先關閉所有的Chrome瀏覽器窗口...

  • 開放終端

  • 須藤,我

  • (密碼)

然後類型

  • 谷歌鉻 - 禁用網絡 - 安全

然後嘗試在URL

1

您必須在頭添加訪問控制允許來源

PHP實例鏈接:

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