CORS issue while building AJAX for elasticsearch on AWS

I have a demo client in javascript trying to search Elasticsearch that works happily on Amazon AWS Cloud.

The point is that I am returning the result (the fiddler shows that it fits perfectly in JSON, as it should be). But the browser amazes me with this:

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.

      

This is the Ajax demo I am using:

  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")
        })
    }

      

To reiterate, Fiddler shows that the result is being returned, but the browser points to the failure method every time. How to enable CORS in this example?

The demo application is a Node.js server.

This is the content of my elasticsearch.yaml file:

The rest of the file is default, which means everything is commented out:

    {
  "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

      

+3


source to share


1 answer


CORS is only implemented and enforced for browsers, not for applications (Fiddler / Rest Client, etc.)

The server needs to resolve domains that will access the service via javascript. Set up your elastic search to do this. Update your http config to do this. Relevant properties: http.cors.enabled

, http.cors.allow-origin

, http.cors.allow-methods

, http.cors.allow-headers

,http.cors.allow-credentials

If you want to do it with vm parameters, use them when starting the process:

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

      



[EDIT]

Extend your .json file by adding the following:

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

      

+10


source







All Articles