【问题标题】:No success callback after $.ajax to Elasticsearch with application/json$.ajax 后没有成功回调到 Elasticsearch 与 application/json
【发布时间】:2017-12-06 10:36:04
【问题描述】:

我在成功使用通过 jQuery 从 ElasticSearch 服务器检索到的 application/json 答案时遇到问题。 Firefox 告诉我,我得到了对 HTTP GET 的有效 200 OK 答案,并且 Firefox 网络监视器可以正确解析 JSON。然而,jQuery 不会调用成功/完成回调,只会调用失败并始终调用。

ElasticSearch 用警告标题告诉我:

299 Elasticsearch-5.4.1-Unknown “REST 请求的内容类型检测已弃用。使用 [Content-Type] 标头指定内容类型。” “格林威治标准时间 2017 年 7 月 3 日星期一 07:59:32”

只要我添加一个

contentType:"application/json; charset=utf-8",

对于 $.ajax() 函数,Elasticsearch 只返回一个空结果。

“有效”但不是回调调用版本的JS代码:

  var postData = {
      "_source": ['title', 'url'],
      "query" : {
          "match": {
              "body": "lorem"
          }
      },
      "highlight" : {
          "pre_tags" : ["<b>"],
          "post_tags" : ["</b>"],
          "fields" : {
              "body" : {
                  "fragment_size" : 150,
                  "number_of_fragments" : 1,
                  "no_match_size": 150
               }
          }
      }
    };
  function docsearch(){
    var data = JSON.stringify(postData);
    $.ajax({
      //contentType:"application/json; charset=utf-8",
      url: "http://127.0.0.1:9200/myindex/_search?pretty=true",
      type: "POST",
      dataType: "json",
      data: data,
      success: function (data) { console.log(data); }
    }).done(function() {
      alert( "success" );
    })
    .fail(function(data) {
      console.log( data );
    })
    .always(function() {
      alert( "complete" );
    });
  };

GET 标头:

Host: 127.0.0.1:9200
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost/
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 208
Origin: http://localhost
DNT: 1
Connection: keep-alive

和结果标题:

Content-Encoding: gzip
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Warning: 299 Elasticsearch-5.4.1-Unknown "Content type detection for rest requests is deprecated. Specify the content type using the [Content-Type] header." "Mon, 03 Jul 2017 07:59:32 GMT"

结果是 JSON:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.25124598,
    "hits" : [
      {
        "_index" : "myindex",
        "_type" : "post",
        "_id" : "10",
        "_score" : 0.25124598,
        "_source" : {
          "title" : "Lorem ipsum"
        },
        "highlight" : {
          "body" : [
            "<b>Lorem</b> ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat"
          ]
        }
      }
    ]
  }
}

失败回调的输出:

09:59:32.973 Object { readyState: 0, getResponseHeader: .ajax/y.getResponseHeader(), getAllResponseHeaders: .ajax/y.getAllResponseHeaders(), setRequestHeader: .ajax/y.setRequestHeader(), overrideMimeType: .ajax/y.overrideMimeType(), statusCode: .ajax/y.statusCode(), abort: .ajax/y.abort(), state: .Deferred/e.state(), always: .Deferred/e.always(), catch: .Deferred/e.catch(), 9 more… }

谁能提供一些见解,为什么jQuery没有调用成功函数?我发现它可能与 Transfer-Encoding: chunked 有关。

谢谢

【问题讨论】:

  • 为什么要发送“var data = JSON.stringify(postData);”字符串格式???而是以数据的形式发送Object:postData。
  • 然后 ElasticSearch 返回错误 406:“Content-Type header [application/x-www-form-urlencoded; charset=UTF-8] is not supported”。并且使用 contentType:"application/json; charset=utf-8" ES 返回一个空结果。
  • 我仍然认为问题在于构建 postData。请检查 ElasticSearch 中的日志,它将为您提供从 UI 触发的确切查询。除此之外,请添加 Content-Type: application/json;
  • 我可能发现了问题。它与 JS 无关,也没有正确反映在我展示的示例中。这是关于缺少 Access-Control-Allow-Origin 标头的,因为弹性搜索服务器实际上驻留在不同的主机上。而且我没有在 Firefox 上启用所有调试输出。 Chromium 确实向我显示了帮助错误消息。

标签: javascript jquery json ajax elasticsearch


【解决方案1】:

抱歉打扰了。答案确实是缺少 Access-Control-Allow-Origin 标头。而且因为是 POST,所以也需要允许:

Header set Access-Control-Allow-Origin "http://elasticsearch-host.domain"
Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"

【讨论】:

    猜你喜欢
    • 2020-01-19
    • 2015-08-26
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多