H
H
hydra_132019-01-09 11:48:44
JavaScript
hydra_13, 2019-01-09 11:48:44

How to use ElasticSearch.js in Chrome browser?

The system (Windows 10 x64) has ElasticSearch 6.2.4 installed.
There is the following html file:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>TestES</title>
  <script>
    function createCORSRequest(method, url) {
      var xhr = new XMLHttpRequest();
      if ("withCredentials" in xhr) {
        xhr.open(method, url, true);
      } else if (typeof XDomainRequest != "undefined") {
        xhr = new XDomainRequest();
        xhr.open(method, url);
      } else {
        xhr = null;
      }
      return xhr;
    }

    function testES(){
      var url = 'http://localhost:9200/';
      // var url = 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html';
      var xhr = createCORSRequest('GET', url);
      if (!xhr) {
        throw new Error('CORS not supported');
      } else {
        xhr.onload = function() {
         var responseText = xhr.responseText;
         console.log('response: ' + responseText);
        };
        xhr.onerror = function() {
          console.log('There was an error!');
        };
        xhr.send();
      }
    }
  </script>
</head>
<body>
  <input type="button" onclick="testES();" value="Click me">
  <div id="result"></div>
</body>
</html>

If you use this file through InternetExplorer - loads the page by default. no problem. But when I try to use Google Chrome in the console, I get the following messages:
Error: Access to XMLHttpRequest at 'http://localhost:9200/' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
There was an error!
Warning: Cross-Origin Read Blocking (CORB) blocked cross-origin response http://localhost:9200/ with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.

How to implement the ability to use this code in the Google Chrome browser?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-01-09
@hydra_13

Enable cors in elastic https://www.elastic.co/guide/en/elasticsearch/refe... and put * in allow origion

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question