使用 elasticsearch.js 时出现 CORS 错误
CORS error when using elasticsearch.js
我正在尝试从本地机器测试索引。我创建了一个带有查询框的简单 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"
}
}
}
}
}
索引.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>
应用.js
angular.module('portal', [
'controllers',
]);
var client = new elasticsearch.Client({
host: 'http://localhost:9200',
apiVersion: '1.3',
});
控制器.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!')}
);
};
}
);
将以下行添加到您的配置 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 的系统上杀死弹性搜索 Java 进程,如本主题响应 https://stackoverflow.com/a/41644614/11079315
为什么一开始就出现跨域错误。你打开你的前端是 file://...吗?完全随机猜测,不确定是否重要。如果你想通过Web服务器运行你的UI,你可以打开一个终端,cd到那个目录并运行'python -m SimpleHTTPServer 7000'。现在您的 UI 正在本地主机: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.allow-credentials: true"。其他默认值应该是明智的。您可能还需要修改"http.cors.max-age"设置,以防缓存旧设置。
使用浏览器中的 js 控制台、网络选项卡查看您要返回的标头。
- 与域在同一台计算机上运行的NODEJS服务器的CORS错误
- XMLHttpRequest在$.ajax工作的地方给出了CORS错误
- 在Polymer中使用核心ajax时出现CORS错误
- 在 AngularJS 和 CodeIgniter3 中开机自检数据时出现 CORS 错误
- AngularJS应用程序中的CORS错误
- Meteor:使用HTTP.get从客户端访问网站(CORS错误)
- Catching'阻止加载混合活动内容'CORS错误
- ajax请求出现CORS错误
- 加载模板时出现AngularJS、Localhost和CORS错误
- 谷歌加 CORS 错误
- 同一域上的 CORS 错误
- 使用 elasticsearch.js 时出现 CORS 错误
- 护照节点.js CORS 错误
- 简单请求中的 Cors 错误:请求标头字段 预检响应中的访问控制-允许-标头不允许授权
- Firefox 和 chrome 的 CORS 错误
- 重定向时出现机架 CORS 错误
- 从localhost使用谷歌联系人api创建联系人时出现CORS错误
- Facebook oEmbed端点CORS错误
- 在Angular中使用解析客户端密钥会导致CORS错误
- 从文件//scheme运行的应用程序出现CORS错误