Enable CORS to Send Cross Domain Request to Elasticsearch using AJAX

           I faced this issue when I used my web application to send AJAX request to get data from Elasticsearch. The web application was deployed in Apache Tomcat (port 8080 was used).

XMLHttpRequest cannot load http://localhost:9200/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

           Elasticsearch by default will not allow cross domain requests. To solve this we have to enable CORS (Cross-Origin Resource Sharing) in Elasticsearch configuration file (ES_HOME/config/elasticsearch.yml). Add the following content to the configuration file.

http.cors.enabled : true

http.cors.allow-origin : "*"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type, Content-Length


Related Links: 

Web Application for Elasticsearch :
  1. ElasticTab – Elasticsearch to Excel Report (Web Application)
Elasticsearch Plugin:
  1. Elasticsearch Plugin To Generate (Save and E-Mail) Excel Reports
  1. Execute Multiple Search Query in Elasticsearch
  2. Monitor Elasticsearch Servers with Shell Script - E-Mail Notification
  3. Execute Multiple Search Query in Elasticsearch
  4. Enable CORS to Send Cross Domain Request to Elasticsearch using AJAX
  5. Elasticsearch – Use Script Filter/Conditon in Aggregation/Sub-Aggreagtion
  6. Elasticsearch - Date/Time(String)  Add/Subtract Duration - Days,Months,Years,Hours,Minutes,Seconds
  7. Elasticsearch Geo-Shape Slow Indexing Performance - Solved
  8. Chrome Elasticsearch Sense Not Working – Solved
  1. Logstash – Process Log File Once and Exit/Stop Logstash After Reading Log File Once
  2. Measure Logstash Performance using Metrics Filter – Issue/Error in Syntax (Unknown setting ‘message’ for stdout)
  3. Logstash – Process Same Log File (File Input) from Beginning/Start
  4. Create Custom Filter/Plugin to Emit New Events Manually in Logstash
Logstash and Elasticsearch:
  1. Query Elasticsearch Cluster in Filter Section when using Logstash
  2. Custom Elasticsearch Index Name/Type Name based on Events in Logstash
MongoDB and Elasticsearch:
  1. Import Data from Mongo DB to Elasticsearch using Elasticsearch River

Leave a Reply