2014-11-07 5 views
4

Amazon AWS Cloud에서 행복하게 실행되는 Elasticsearch에서 검색을 시도하는 javascript의 데모 클라이언트가 있습니다.AWS에서 AJAX를 elasticsearch로 만들 때 CORS 문제가 발생합니다.

결과는 내가 결과를 다시 얻는다는 것입니다 (피들러는 JSON에서 멋지게 나타남을 보여줍니다). 그러나 브라우저는이 날 타격 :

XMLHttpRequest cannot load http://ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:9200/pekara/hljeb/_search. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7000' is therefore not allowed access. 

이 내가 사용하는 데모 아약스입니다 :

var searchString = "http://ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:9200/pekara/hljeb/_search"; 

     debugger; 
     $.ajax({ 
      url: searchString, 
      type: 'POST', 
      contentType: 'application/json; charset=UTF-8', 
      dataType: "json", 
      crossDomain: true, 
      data: JSON.stringify(data), 
      success: function (data) { 
       console.log("And this is success: " + data); 

       $("#contentholder").text(data); 
      } 
     }).fail(function (error) { 
      console.log("Search Failed") 
     }) 
    } 

그래서 반복, 피들러는 결과가 돌아 오면 보여 주지만, 브라우저 점은 모든 방법을 실패 한 번. 이 예에서 CORS를 해결하는 방법은 무엇입니까?

데모 애플리케이션은 Node.js의 서버입니다.

이 내 elasticsearch.yaml 파일의 내용입니다 : 유일한 브라우저가 아닌 애플리케이션에

{ 
    "cluster.name": "Mycluster", 
    "node.name": "My-node", 
    "cloud": { 
    "aws": { 
     "access_key": "xxxxxxxxxxxxxxxxxxxxxx", 
     "secret_key": "xxxxxxxxxxxxxxxxx" 
    } 
    }, 
    "discovery": { 
    "type": "ec2", 
      "ec2" : { 
     "groups": "Elastic-Search-GROUP" 
    } 
} 
} 


http.cors.allow-origin: true, 
http.cors.allow-methods: true, 
http.cors.allow-headers: true, 
http.cors.allow-credentials: true, 
http.cors.enabled: true 

답변

10

CORS이 구현 및 적용 : 기본, 즉 모든 것이 주석

파일의 나머지는 (Fiddler/Rest Client 외)

서버가 자바 스크립트를 통해 서비스에 액세스 할 도메인을 허용해야합니다. 그렇게하기 위해 신축성있는 검색을 구성하십시오. 업데이트하려면 http config을 업데이트하십시오.

elasticsearch -Des.http.<property1>=<val1> -Des.http.<property2>=<val2> ... 

[편집]

: 관련 속성 : 당신이 VM 매개 변수를 통해 작업을 수행하려면 프로세스를 시작하는 동안 http.cors.enabled, http.cors.allow-origin, http.cors.allow-methods, http.cors.allow-headers, http.cors.allow-credentials

,이를 사용 다음을 추가하여 .json 파일을 확장하십시오.

"http": { 
    "cors.enabled" : true, 
    "cors.allow-origin": "*" 
} 
+0

그래서 이러한 속성을 elasticsearch.yaml 파일에 추가합니까? –

+0

예, 좋을 것입니다. –

+0

우리가 말한대로 그것을 시도 :). 결과 게시 즉시 –