首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用elasticsearch.js时CORS错误

使用elasticsearch.js时CORS错误
EN

Stack Overflow用户
提问于 2015-01-23 00:12:59
回答 2查看 8.5K关注 0票数 2

我正在尝试从本地机器上测试一个索引。我创建了一个带有查询框的简单HTML页面,该查询框使用elasticsearch.js客户端将查询发送到ES。索引和浏览器都在我的桌面上,所以不应该出现跨源问题,但我一直收到一个错误,即:

代码语言:javascript
复制
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,但是得到了相同的错误。以下是索引的设置:

代码语言:javascript
复制
{
    "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

代码语言:javascript
复制
<!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>&nbsp;
                <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

代码语言:javascript
复制
angular.module('portal', [
    'controllers',
]);

var client = new elasticsearch.Client({
    host: 'http://localhost:9200',
    apiVersion: '1.3',

});

controller.js

代码语言:javascript
复制
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!')}
            );
        };
    }
);
EN

回答 2

Stack Overflow用户

发布于 2020-12-15 15:40:13

在config yml文件中添加以下行

代码语言:javascript
复制
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过程。

票数 3
EN

Stack Overflow用户

发布于 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选项卡查看您要返回的标头。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28100985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档