Bonsai Elasticsearch附加组件:401未经授权的错误

时间:2017-06-24 08:42:05

标签: ajax firebase elasticsearch heroku bonsai-elasticsearch

为了部署我使用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'
        });

    });

});

我使用了我的盆景网址的正确用户名和密码,但它无效。

控制台错误: enter image description here

Chrome控制台错误:enter image description here

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"
      }
    } ]
  }
}

问题:我缺少什么?

1 个答案:

答案 0 :(得分:1)

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