# XMLHttpRequest is not working

**URL:** <https://discuss.elastic.co/t/xmlhttprequest-is-not-working/172610>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security\
**Created:** [March 16, 2019, 12:35am UTC](https://discuss.elastic.co/t/xmlhttprequest-is-not-working/172610 "2019-03-16T00:35:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danreut89](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@danreut89](https://discuss.elastic.co/u/danreut89)\
**Post date:** [March 16, 2019, 12:35am UTC](https://discuss.elastic.co/t/xmlhttprequest-is-not-working/172610/1 "2019-03-16T00:35:12Z")

</div>

I am new to the elasticsearch and JavaScript development. I am trying to use the code below, so I can do regular REST calls and get JSON back.

Here is my code that I am using in section

function myFunction() {

var xhr = new XMLHttpRequest();  
xhr.open('GET', '[http://d77511xxx.us-central1.gcp.cloud.es.io:9243](http://d77511xxx.us-central1.gcp.cloud.es.io:9243)', true);  
xhr.withCredentials = true;  
xhr.setRequestHeader('Content-Type', 'application/json');  
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('user:password'));  
xhr.send('{"query":{"match\_all":{}}}');  
}

Here is my additional setting that I am using in elasticsearch.yml

http.cors.enabled: true  
http.cors.allow-origin: "/.\*/"  
http.cors.allow-credentials: true  
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"

But for some reason I am getting 401 or other issues.

Here is my ElasticSearch Server Link: [http://d77511xxx.us-central1.gcp.cloud.es.io:9243](http://d77511xxx.us-central1.gcp.cloud.es.io:9243)

---

<div class="post-metadata">

**Author:** ![Alex\_Piggott](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_piggott/32/11053_2.png) [@Alex\_Piggott](https://discuss.elastic.co/u/Alex_Piggott)\
**Post date:** [March 16, 2019, 1:13am UTC](https://discuss.elastic.co/t/xmlhttprequest-is-not-working/172610/2 "2019-03-16T01:13:35Z")

</div>

Hi @danreut89

Note that I removed some chars from your cluster id (we recommend only including the first 6). Note also that we offer direct support for cloud - login and visit [https://cloud.elastic.co/help](https://cloud.elastic.co/help)

Very quickly though the first thing I noticed is that you have `http` but `9243` is an https port. So try `https://` instead.

401 is normally an bad user/password combo. Are you using the `elastic` and the password generated when you created the cluster (or when you reset it from the security tab)

eg Does the same call work from a `curl`, if you do

`curl -vv -u user:password 'https://d77511xxx.us-central1.gcp.cloud.es.io:9243' -d '{ "query": { "match_all": {} }' -H 'Content-Type: application/json'`

(with the correct user+password and the full cluster id)

---

<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:** [March 30, 2019, 1:13am UTC](https://discuss.elastic.co/t/xmlhttprequest-is-not-working/172610/3 "2019-03-30T01:13:36Z")

</div>

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