首页 文章

试图用axios连接到elasticsearch

提问于
浏览
0

我是axios的新手,我正在尝试将我的弹性搜索 Cloud 与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);

我不知道在哪里输入我的用户名和密码?

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

谢谢你的帮助 !

1 回答

  • 0

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

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

    请注意,您可能需要将 withCredentials 参数设置为 true ,具体取决于您要定位的后端的配置 .

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

相关问题