繁体   English   中英

如何使用 auth 获取 elasticsearch api

[英]How to fetch elasticsearch api with auth

我尝试使用带有基本身份验证的 javascript 来获取弹性 api,但是有错误表明访问控制允许标头不允许请求标头字段授权,是弹性搜索 api 有问题还是我的代码有问题? 我已经在 elastic 上设置了 enable cors,我尝试 curl 使用 auth 获取弹性数据并且它有效,获取代码是否错误?

控制台错误: 在此处输入图像描述

获取代码:

        fetch('http://192.168.150.220:9900/', {method:'GET', 
        headers: {'Authorization': 'Basic ' + btoa('em_user:3md@t@2o22')}})
        .then(response => response.json())
        .then(json => console.log(json));

要从浏览器使用 fetch,您必须在 ElasticSearch 配置中允许跨域请求:

 http.cors.enabled : true
 http.cors.allow-origin: "*"
 http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
 http.cors.allow-headers: X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization
 http.cors.allow-credentials: true

资源:

https://www.elastic.co/guide/en/elasticsearch/reference/8.2/modules-network.html https://www.elastic.co/guide/en/cloud-enterprise/current/ece-configure-cors .html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM