I'm having a problem with successfully using the application/json answer retrieved from an ElasticSearch server with jQuery. Firefox tells me that I'm getting a valid 200 OK answer to the HTTP GET and the Firefox Network Monitor can correctly parse the JSON. jQuery however does not call the success / done callback, only fail and always are called.
ElasticSearch is telling me with a Warning header:
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"
As soon as I add a
contentType:"application/json; charset=utf-8",
to the $.ajax() function, Elasticsearch only returns an empty result.
The JS code for the "valid" but not callback-calling version:
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" );
});
};
The GET headers:
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
And result headers:
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"
And the result 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"
]
}
}
]
}
}
The output of the fail callback:
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… }
Can anyone provide some insight, why jQuery is not calling the success function? I found that it may be related to Transfer-Encoding: chunked.
Thanks