【问题标题】:Tried to connect to elasticsearch with axios尝试用axios连接elasticsearch
【发布时间】:2017-09-14 09:49:48
【问题描述】:

我是 axios 的新手,我正在尝试将我的弹性搜索云与 axios 连接,我得到:错误 401 未授权。

这是我在 vue.js 中的代码:

 axios.get('https://username:password@url:9243/elastictest/apps/_search?pretty',{
        params:{
             source: JSON.stringify(query),
            source_content_type: 'application/json' ,

        },

       }).then(response =>this.skills =response.data.hits.hits);

我不确定我的用户名和密码应该放在哪里?

如果您有任何想法或解决方案?

感谢您的帮助!

【问题讨论】:

  • 你的 elasticsearch 在哪里运行?在本地主机上,还是像 Elastic Cloud 这样的 SaaS?

标签: javascript elasticsearch vue.js axios


【解决方案1】:

我假设,您的浏览器不尊重通过 url 传递的凭据。您可以通过设置auth 属性,如the axios docs 中所列,直接在配置对象(您还可以在其中定义参数)中传递身份验证信息。

auth: {
  username: 'username',
  password: 'password'
}

请注意,您可能需要将withCredentials 参数设置为true,具体取决于您所针对的后端的配置。

由于此代码看起来像是直接从浏览器访问 elasticsearch 实例,您应该知道您的数据库随后会暴露给所有客户端,并且身份验证信息也可以从您的客户端 javascript 中读取。这可能不是你想要的。

【讨论】:

    猜你喜欢
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多