我正在尝试从本地机器上测试一个索引。我创建了一个带有查询框的简单HTML页面,该查询框使用elasticsearch.js客户端将查询发送到ES。索引和浏览器都在我的桌面上,所以不应该出现跨源问题,但我一直收到一个错误,即:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9200/portal/book/_search?q=title%3Ahistoire. This can be fixed by moving the resource to the same domain or enabling CORS.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9200/. This can be fixed by moving the resource to the same domain or enabling CORS.我试过启用CORS,但是得到了相同的错误。以下是索引的设置:
{
"portal": {
"settings": {
"index": {
"creation_date": "1421788614558",
"uuid": "jg-iHjnSTDGHODY0_x4Ysw",
"number_of_replicas": "1",
"http": {
"cors": {
"enabled": "true",
"allow-origin": "/(http://)?localhost(:[0-9]+)?/"
}
},
"number_of_shards": "5",
"version": {
"created": "1040299"
}
}
}
}
}index.html
<!DOCTYPE html >
<html ng-app="portal">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/elasticsearch/elasticsearch.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-controller="SearchCtrl">
<div class="container-fluid">
<div class="row-fluid">
<span class="span3">
<input class="input-block-level" ng-model="queryTerm" type="text">
</span>
<button ng-click="search()" class="btn" type="button">Search</button>
</div>
<div class="row-fluid">
Found {{ results.hits.total }}
<div class="span4">
<li ng-repeat="doc in results.hits.hits">
{{ doc._source.title }}
</li>
</div>
</div>
</div>
</body>
</html>app.js
angular.module('portal', [
'controllers',
]);
var client = new elasticsearch.Client({
host: 'http://localhost:9200',
apiVersion: '1.3',
});controller.js
angular.module('controllers', []).controller('SearchCtrl',
function($scope) {
$scope.search = function(query) {
$scope.results = client.search({
index: 'portal',
type: 'book',
q: 'title:' + $scope.queryTerm
}, function (error, response) {console.log('could not execute query!')}
);
};
}
);发布于 2020-12-15 15:40:13
在config 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
http.cors.allow-credentials: true并在基于unix的系统https://stackoverflow.com/a/41644614/11079315上演示了杀死弹性搜索java过程。
发布于 2015-02-11 15:39:05
我不知道你为什么要从一个跨域错误开始。您是否将前端打开为file://....??完全是随机猜测,也不确定这有什么关系。如果您是并希望通过web服务器运行您的UI,您可以打开一个终端,将cd放入该dir,并运行'python -m SimpleHTTPServer 7000‘。现在,您的UI运行在localhost:7000上。
有关CORS设置,请参阅http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html,如果您正在使用Chrome,http://www.williamjohnbert.com/2013/06/allow-cors-with-localhost-in-chrome/可能会有所帮助。
在您的配置中,您至少应该设置'http.cors.enabled: true‘和’http.cors.lox-凭据: true‘。其他违约应该是明智的。您还可能希望修改“http.cors.max-age”设置,以防止缓存旧设置。
使用浏览器中的js控制台,net选项卡查看您要返回的标头。
https://stackoverflow.com/questions/28100985
复制相似问题