使用elasticsearch.js时CORS错误 [英] CORS error when using elasticsearch.js
问题描述
我想测试来自我的本地机器的索引。我创建了一个简单的HTML页面发送使用elasticsearch.js客户端查询到ES的查询框。无论是指数和浏览器是我的桌面上,所以不应该有一个跨起源问题,但我不断收到指出的错误:
跨域请求阻止:同源策略不允许在http读取远程资源://本地主机:9200 /门/电子书/ _search Q =标题%3Ahistoire。这可以由资源移动到相同域或启用CORS固定。跨域请求阻止:同源策略不允许在http读取远程资源://本地主机:9200 /。这可以由资源移动到相同域或启用CORS固定。
我试图使CORS,但得到了同样的错误。以下是该指数的设置:
{
门户:{
设置:{
指数:{
CREATION_DATE:1421788614558,
的「uuid:JG-iHjnSTDGHODY0_x4Ysw
number_of_replicas:1,
的http:{
CORS:{
已启用:真,
允许原产地:/(HTTP://)本地主机?(:[0-9] +)?/
}
},
number_of_shards:5,
版本:{
创造:1040299
}
}
}
}
}
的index.html
<!DOCTYPE HTML>
< HTML NG-应用=门户>
< HEAD>
< META HTTP-EQUIV =Content-Type的CONTENT =text / html的;字符集= UTF-8>
<链接rel =stylesheet属性HREF =bower_components /引导/距离/ CSS / bootstrap.min.css> &所述; SCRIPT SRC =bower_components /角度/ angular.min.js>&下; /脚本>
&所述; SCRIPT SRC =bower_components / elasticsearch / elasticsearch.min.js>&下; /脚本> <脚本SRC =JS / app.js>< / SCRIPT>
<脚本SRC =JS / controllers.js>< / SCRIPT>
< /头> <机身NG控制器=SearchCtrl>
< DIV CLASS =集装箱液>
< DIV CLASS =排液>
<跨度类=span3>
<输入类=输入块级NG模型=queryTerm类型=文本>
< / SPAN>&安培; NBSP;
<按钮NG点击=搜索()级=BTN类型=按钮>搜索和LT; /按钮>
< / DIV> < DIV CLASS =排液>
发现{{results.hits.total}}
< DIV CLASS =span4>
<李NG重复=文档中results.hits.hits>
{{doc._source.title}}
< /李>
< / DIV>
< / DIV>
< / DIV>
< /身体GT;
< / HTML>
app.js
angular.module(门户,[
控制器,
]);VAR的客户=新elasticsearch.Client({
主持人:HTTP://本地主机:9200',
apiVersion:'1.3',});
controller.js
angular.module('控制器',[])。控制器('SearchCtrl',
功能($范围){ $ scope.search =功能(查询){
$ scope.results = client.search({
索引:门户,
类型:'书',
问:'标题:'+ $ scope.queryTerm
},功能(错误响应){执行console.log('无法执行的查询!')}
);
};
}
);
我不知道为什么你得到一个跨域错误开始。你打开你的前端为文件:// ....?完全随机的猜测,不知道它的问题。如果你是,想通过Web服务器来运行你的UI,你可以打开一个终端,cd进入这个目录并运行蟒蛇-m SimpleHTTPServer 7000。现在你的用户界面是本地主机上运行: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/~~V 可能的帮助。
在你的配置,你至少应该设置'http.cors.enabled:真正的和http.cors.allow的凭据:真。其他的缺省值应该是明智的。您可能还需要修改http.cors.max时代'的情况下,制定旧的设置会被缓存。
使用JS的控制台,网络标签在你的浏览器,看看有什么头可以被找回。
I am trying to test an index from my local machine. I created a simple HTML page with a query box that sends the query to ES using the elasticsearch.js client. Both the index and the browser are on my desktop, so there shouldn't be a cross origin problem, but I keep getting an error that states:
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.
I tried enabling CORS, but get the same error. Here are the index's settings:
{
"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!')}
);
};
}
);
I don't know why you are getting a cross domain error to begin with. Are you opening your front end as file://.... ? Totally random guess and not sure it matters. If you are and want to run your UI through a web server, you can open a terminal, cd into that dir and run 'python -m SimpleHTTPServer 7000'. Now your UI is running on localhost:7000.
For CORS settings, see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html and, if you are using Chrome, http://www.williamjohnbert.com/2013/06/allow-cors-with-localhost-in-chrome/ might help.
In your config you should at least set 'http.cors.enabled: true' and 'http.cors.allow-credentials: true'. The other defaults should be sensible. You may also want to modify the 'http.cors.max-age' setting in case old settings are cached.
Use the js console, net tab in your browser to see what headers you are getting back.
这篇关于使用elasticsearch.js时CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!