CORS问题,同时使AJAX在AWS上弹性搜索 [英] CORS issue while making AJAX to elasticsearch on AWS

查看:338
本文介绍了CORS问题,同时使AJAX在AWS上弹性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中有演示客户端,尝试在Amazon AWS Cloud上快速运行的Elasticsearch上搜索。



这是我得到的结果fiddler显示它在JSON中很好,因为它应该是)。但浏览器打我这样:

  XMLHttpRequest无法加载http:// ec2-xx-xxx-xxx-xxx。 us-west-2.compute.amazonaws.com:9200/pekara/hljeb/_search。在所请求的资源上没有Access-Control-Allow-Origin头。因此,不允许原始'http:// localhost:7000'访问。 

这是我使用的演示Ajax:

  var searchString =http://ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:9200/pekara/hljeb/_search ; 

debugger;
$ .ajax({
url:searchString,
type:'POST',
contentType:'application / json; charset = UTF-8',
dataType :json,
crossDomain:true,
data:JSON.stringify(data),
success:function(data){
console.log :+ data);

$(#contentholder).text(data);
}
})。fail(function(error){
console.log(Search Failed)
})
}



重复,Fiddler显示结果回来,但浏览器指向失败的方法每一次。
此示例中如何解决CORS?



演示应用程序是Node.js上的服务器。



这是我的elasticsearch.yaml文件的内容:



文件的其余部分是默认的,意思是一切都被注释掉:

  {
cluster.name:Mycluster,
node.name:My-node,
cloud:{
aws:{
access_key:xxxxxxxxxxxxxxxxxxxxxx,
secret_key:xxxxxxxxxxxxxxxxx
}
},
discovery:{
type:ec2,
ec2:{
groups:Elastic-Search-GROUP
} b $ b}
}


http.cors.allow-origin:true,
http.cors.allow-methods:true,
http .cors.allow-headers:true,
http.cors.allow-credentials:true,
http.cors.enabled:true


解决方案

CORS 仅适用于浏览器而不适用于应用程序(Fiddler / Rest Client等)



您的服务器需要允许通过javascript访问服务的域。配置弹性搜索以执行此操作。请更新 http配置。相关属性: http.cors.enabled http.cors.allow-origin http .cors.allow-methods http.cors.allow-headers http.cors.allow-credentials



如果您想通过vm参数进行操作,请在启动过程时使用这些参数:

  elasticsearch -Des.http。< property1> =< val1> -Des.http。< property2> =< val2> ... 


通过添加以下内容扩展.json文件:

 http:{
cors.enabled:true,
cors.allow-origin:*
}


I have demo client in javascript that attempts to do a search on Elasticsearch that runs happily on Amazon AWS Cloud.

Thing is that I am getting the result back (fiddler shows it comes nicely in JSON, as it should be). But the browser is hitting me with this:

XMLHttpRequest cannot load http://ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:9200/pekara/hljeb/_search. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7000' is therefore not allowed access.

This is the demo Ajax that I use:

  var searchString = "http://ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:9200/pekara/hljeb/_search";

        debugger;
        $.ajax({
            url: searchString,
            type: 'POST',
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            crossDomain: true,
            data: JSON.stringify(data),
            success: function (data) {
                console.log("And this is success: " + data);

                $("#contentholder").text(data);
            }
        }).fail(function (error) {
            console.log("Search Failed")
        })
    }

So to repeat, Fiddler shows the result comes back, but browser points to fail method every single time. How to resolve CORS in this example?

The demo application is being server on Node.js.

This is content of my elasticsearch.yaml file:

The rest of the file is default, meaning everything is commented out:

    {
  "cluster.name": "Mycluster",
  "node.name": "My-node",
  "cloud": {
    "aws": {
      "access_key": "xxxxxxxxxxxxxxxxxxxxxx",
      "secret_key": "xxxxxxxxxxxxxxxxx"
    }
  },
  "discovery": {
    "type": "ec2",
          "ec2" : {
        "groups": "Elastic-Search-GROUP"
   }
 }
}


http.cors.allow-origin: true,
http.cors.allow-methods: true,
http.cors.allow-headers: true,
http.cors.allow-credentials: true,
http.cors.enabled: true

解决方案

CORS is implemented and applicable only to browsers and not apps (Fiddler/Rest Client et al)

Your server needs to allow the domains that are going to access the service via javascript. Configure your elastic search to do so. Update the http config to do so. Relevant properties: http.cors.enabled, http.cors.allow-origin, http.cors.allow-methods, http.cors.allow-headers, http.cors.allow-credentials

If you want to do it via the vm parameters, use these while starting the process:

elasticsearch -Des.http.<property1>=<val1> -Des.http.<property2>=<val2> ...

[EDIT]

Extend your .json file by adding this:

"http": {
    "cors.enabled" : true,
    "cors.allow-origin": "*"
}

这篇关于CORS问题,同时使AJAX在AWS上弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆