# Connexion problem with elasticsearch and ajax

**URL:** <https://discuss.elastic.co/t/connexion-problem-with-elasticsearch-and-ajax/80677>\
**Category:** Elasticsearch\
**Created:** [March 30, 2017, 12:35pm UTC](https://discuss.elastic.co/t/connexion-problem-with-elasticsearch-and-ajax/80677 "2017-03-30T12:35:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrilabarre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henrilabarre/32/16909_2.png) [@henrilabarre](https://discuss.elastic.co/u/henrilabarre)\
**Post date:** [March 30, 2017, 12:35pm UTC](https://discuss.elastic.co/t/connexion-problem-with-elasticsearch-and-ajax/80677/1 "2017-03-30T12:35:05Z")

</div>

Hi all,

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

```
$.ajax({
				url: "http://localhost:9200/_search?q=dep:10",
				type: 'GET',
				dataType: 'json',
				crossDomain: true,
				success: function(data) {
					console.log('Success');
					$(data.hits.hits).each(function(index, hit) {
						console.log(hit._source.items);
					});
				},
				error: function(jqXHR, textStatus, errorThrown) {
					console.log(textStatus);
					console.log(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

```
net::ERR_CONNECTION_REFUSED

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
Origin:https://domain.com
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
q:dep:10

```

I try this to check the setup

```
root@host4:~# curl -H "User-Agent: Mozilla" -H "Origin: https://domain.com" -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!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 31, 2017, 7:14am UTC](https://discuss.elastic.co/t/connexion-problem-with-elasticsearch-and-ajax/80677/2 "2017-03-31T07:14:14Z")

</div>

Hey,

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).

--Alex

---

<div class="post-metadata">

**Author:** ![henrilabarre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henrilabarre/32/16909_2.png) [@henrilabarre](https://discuss.elastic.co/u/henrilabarre)\
**Post date:** [March 31, 2017, 7:31am UTC](https://discuss.elastic.co/t/connexion-problem-with-elasticsearch-and-ajax/80677/3 "2017-03-31T07:31:30Z")

</div>

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 😉

What do you think of this solution?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 31, 2017, 7:37am UTC](https://discuss.elastic.co/t/connexion-problem-with-elasticsearch-and-ajax/80677/4 "2017-03-31T07:37:28Z")

</div>

+1000, when you dont expose Elasticsearch to the internet 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 28, 2017, 7:37am UTC](https://discuss.elastic.co/t/connexion-problem-with-elasticsearch-and-ajax/80677/5 "2017-04-28T07:37:32Z")

</div>

Closed on April 28, 2017, 7:37am UTC

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