Amazon web services 在AWS上进行AJAX到elasticsearch时出现CORS问题
我有一个javascript演示客户端,它试图在AmazonAWS云上运行的Elasticsearch上进行搜索 事情是,我得到了结果(fiddler显示它在JSON中很好地实现了,这是应该的)。但浏览器给我的印象是:Amazon web services 在AWS上进行AJAX到elasticsearch时出现CORS问题,amazon-web-services,
elasticsearch,cors,Amazon Web Services,
elasticsearch,Cors,我有一个javascript演示客户端,它试图在AmazonAWS云上运行的Elasticsearch上进行搜索 事情是,我得到了结果(fiddler显示它在JSON中很好地实现了,这是应该的)。但浏览器给我的印象是: XMLHttpRequest cannot load http://ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:9200/pekara/hljeb/_search. No 'Access-Control-Allow-Ori
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.
这是我使用的演示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("And this is success: " + data);
$("#contentholder").text(data);
}
}).fail(function (error) {
console.log("Search Failed")
})
}
所以重复一下,Fiddler显示结果返回,但浏览器每次都指向fail方法。
在本例中如何解析CORS
演示应用程序正在Node.js上运行
这是my 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"
}
}
}
http.cors.allow-origin: true,
http.cors.allow-methods: true,
http.cors.allow-headers: true,
http.cors.allow-credentials: true,
http.cors.enabled: true
实现并仅适用于浏览器,而不适用于应用程序(Fiddler/Rest客户端等)
您的服务器需要允许将通过javascript访问服务的域。为此配置弹性搜索。更新以执行此操作。相关属性: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> ...
因此,我将这些属性添加到elasticsearch.yaml文件中?是的,这样会更好。请边说边尝试:)。将在ASAPI将这些属性添加到elasticsearch.yaml并重新启动服务器后发布结果。还是一样的问题,等等。现在,
*
表示允许所有来源访问您的es服务。但是,您应该通过限制域来保护它。为此,请在中仅指定一组域。好吧,除非你想阻止他们。
"http": {
"cors.enabled" : true,
"cors.allow-origin": "*"
}