【问题标题】:Bonsai Elasticsearch add-on: 401 Unauthorised ErrorBonsai Elasticsearch 插件:401 未经授权的错误
【发布时间】:2017-06-24 08:42:04
【问题描述】:

为了部署我的使用 Elasticsearch 的 firebase 应用程序,我正在考虑使用 Heroku Bonsai Add-on。

一切都设置好了,问题是我在传递 Ajax 请求时遇到未经授权的错误。完全相同的代码在本地 (link) 上运行顺利,但在部署时却没有。

Ajax.jx

$(function() {
    $('#message').keyup(function() {
        var query = {
            "query": {
                "match": {
                    "song": {
                        "query": $("#message").val(),
                        "operator": "and"
                    }
                }
            }
        };

        $.ajax({
            type: "POST",
            url: "https://<username>:<password>@xxxwood-5671445.xx-xxxx-1.bonsaisearch.net/firebase/_search",
            contentType: 'application/json',
            crossDomain: true,
            data: JSON.stringify(query),
            success: searchSuccess,
            dataType: 'json'
        });

    });

});

我为我的 Bonsai URL 使用了正确的用户名和密码,但它不起作用。

控制台错误:

Chrome 控制台错误:

cURL 虽然可以正常工作:

[~]$ curl -XGET 'https://<username>:<password>@dogwood-5671445.xx-xxxx-1.bonsaisearch.net/firebase/_search?pretty' -H 'Content-Type: application/json' -d'
{"query": {"match": {"song": {"query": "i am in", "operator": "and"}}}}'

cURL 结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.4048493,
    "hits" : [ {
      "_index" : "firebase",
      "_type" : "song",
      "_id" : "0001",
      "_score" : 1.4048493,
      "_source" : {
        "song" : "i am in california",
        "song_name" : "hello",
        "song_url" : "https://xxx.xx/xxxx"
      }
    } ]
  }
}

问题:我错过了什么?

【问题讨论】:

  • 如果您像这样在标头中附加身份验证凭据会发生什么?:stackoverflow.com/a/5507289/2592585
  • 如果您可以将其添加为答案,以便我接受。正如您正确指出的那样,这是一个标题问题。谢谢。
  • 另外,如果您只是从 Ajax 搜索,那么 Bonsai 允许您启用不需要凭据的“只读”模式。这样您就不会向公众分发您的凭据:-)

标签: ajax firebase elasticsearch heroku bonsai-elasticsearch


【解决方案1】:

很可能 Ajax 没有从 URL 传递凭据。通过 HTTP 标头附加身份验证,如下所示:How to use Basic Auth with jQuery and AJAX?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    相关资源
    最近更新 更多