问题
I installed ElasticSearch on my Ubuntu server and it seems to be working correctly when I do the cURL command. However, when I use the ElasticSearch URI using Ajax I get the following error: net::ERR_BLOCKED_BY_CLIENT
Here's how my Ajax request looks like:
const QUERY_URL = "http://localhost:9200/topics/_search?q=name:" + QUERY
+ "*&sort=follower_count:desc&size=5";
$.ajax({
type: 'GET',
url: QUERY_URL,
success: function (data) {
console.log(data);
},
error: function (xhr) {
if (xhr.status === 0) {
showSnackBarMessage("Can't communicate with server");
}
}
});
I tried using POST
instead of GET
. I also tried changing localhost to its I.P equivalent. However, nothing seems to be working. I keep on getting the same error. Is there a way to solve this without opening my ElasticSearch API to the world?
回答1:
HINT: NEVER EXPOSE ELASTIC TO THE INTERNET DIRECTLY DUE TO SECURITY REASONS!
The error is in the client (your browser/your code) because the CORS settings prevents you to call resources from another host than your website (the port might be different, right?). For Example, your website is running at localhost:8080 and you need to ajax localhost:9200 will lead into net::ERR_BLOCKED_BY_CLIENT
You need to handle CORS in your client/jquery properly and maybe define some CORS-related settings in elasticsearch HTTP network
来源:https://stackoverflow.com/questions/63462868/elasticsearch-not-working-with-same-origin