Connexion problem with elasticsearch and ajax

Hi all,

I plan to get elasticsearch query made by javascript ajax and I made my call like that

				url: "http://localhost:9200/_search?q=dep:10",
				type: 'GET',
				dataType: 'json',
				crossDomain: true,
				success: function(data) {
					$(data.hits.hits).each(function(index, hit) {
				error: function(jqXHR, textStatus, errorThrown) {

And I put this in my elasticsearch.yml

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, Authorization

But I have this error in my console


Request URL:http://localhost:9200/_search?q=dep:10
Referrer Policy:no-referrer-when-downgrade
Request Headers
Provisional headers are shown
Accept:application/json, text/javascript, */*; q=0.01
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.33 Safari/537.36
Query String Parameters
view source
view URL encoded

I try this to check the setup

root@host4:~# curl -H "User-Agent: Mozilla" -H "Origin:" -i localhost:9200
HTTP/1.1 200 OK
access-control-allow-origin: *
content-type: application/json; charset=UTF-8
content-length: 331

  "name" : "host4",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "cvaKK5yDSC-Dt_wiSHc4Eg",
  "version" : {
    "number" : "5.2.2",
    "build_hash" : "f9d9b74",
    "build_date" : "2017-02-24T17:26:45.835Z",
    "build_snapshot" : false,
    "lucene_version" : "6.4.1"
  "tagline" : "You Know, for Search"

Thanks for your help, I'm really stuck on this!


so the connection refused error sounds as if the browser cannot even connect to Elasticsearch. Can you try the IP instead of localhost? Just want to make sure there are no mixups between ipv4 and ipv6 (though Elasticsearch should handle this).

That said, you should not put this application anywhere into the internet, as you would need to expose Elasticsearch into the internet (dont do this).


Hi alexander

Thanks for your answer, I really appreciate that!

I already try with an IP but my ES doesn't seem to work well and as you said it's not secure at all.

After three days of search I finally trick the ajax with using a request to a php page with a GET or POST parameter and on this page use php curl to query ES, I know this is a bit complicate but it work and bypass all CORS problem :wink:

What do you think of this solution?

+1000, when you dont expose Elasticsearch to the internet :slight_smile:


This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.