hydra_13
@hydra_13
Пограммист - любитель)

Как использовать ElasticSearch.js в браузере Chrome?

В системе (Windows 10 x64) установлен ElasticSearch 6.2.4
Имеется следующий html-файл:
<!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>

Если использовать этот файл через InternetExplorer - загружает страницу по-умол. без проблем. Но при попытке использования Google Chrome в консоли выдает следующие сообщения:
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.

Как реализовать возможность использования данного кода в браузере Google Chrome?
  • Вопрос задан
  • 311 просмотров
Решения вопроса 1
@grinat
Включить cors в эластике https://www.elastic.co/guide/en/elasticsearch/refe... и в allow origion поставить *
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы