0

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

一切都设置好了,问题是我在传递 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"}}}}'

卷曲结果:

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

问题:我错过了什么?

4

1 回答 1

1

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

于 2017-06-25T22:30:04.773 回答