gpt4 book ai didi

amazon-web-services - 将 AJAX 制作为 AWS 上的 Elasticsearch 时出现 CORS 问题

转载 作者:行者123 更新时间:2023-11-29 02:51:05 24 4
gpt4 key购买 nike

我有一个 javascript 演示客户端,它尝试在 Elasticsearch 上进行搜索,该搜索在 Amazon AWS Cloud 上运行良好。

事情是我正在取回结果(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-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 显示返回结果,但浏览器每次都指向失败方法。本例如何解析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"
}
}
}


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 访问服务的域。为此配置您的 Elasticsearch 。更新 http config这样做。相关属性:http.cors.enabledhttp.cors.allow-originhttp.cors.allow-methodshttp .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": "*"
}

关于amazon-web-services - 将 AJAX 制作为 AWS 上的 Elasticsearch 时出现 CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com