我尝试使用以下命令通过Docker设置我的elasticsearch服务器:
docker run \
-p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
-e "xpack.security.enabled=false" \
-e "http.cors.allow-credentials=true" \
-e "http.cors.allow-headers=Access-Control-Allow-Origin,x-csrf-token,authorization,content-type,accept,origin,x-requested-with" \
-e "http.cors.allow-origin=http://localhost:3100" \
docker.elastic.co/elasticsearch/elasticsearch:8.6.1
当我使用curl
向它发出请求时,它工作:
myapp - (main) $ curl -XGET "http://localhost:9200/_search" -H "Origin: http://example.com" -H "Content-Type: application/json" -d'
{
"query": {
"match_all" : {}
}
}'
{"took":0,"timed_out":false,"_shards":{"total":0,"successful":0,"skipped":0,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"max_score":0.0,"hits":[]}}%
但是,在我的react代码中,我尝试使用axios发出相同的请求,但得到的只是CORS错误
try {
const res = await axios.get('http://localhost:9200/_search', {
data: JSON.stringify({
query: {
match_all: {},
},
}),
headers: {
'Content-Type': 'application/json',
},
});
console.log('response: ', res.data);
} catch (e) {
console.log('eeeeeeeeeee', e);
}
我该怎么补救呢?
1条答案
按热度按时间xpszyzbs1#
Docker运行命令中缺少某些部分。您可以尝试按照示例操作。