Using ElasticSearch from the browser fails on OPTIONS request

Refresh

November 2018

Views

301 time

2

I am using jQuery's ajax method to make a POST request to my Elastic's _search API.

When passing parameters in the query, all is good:

$.ajax({
  url: '//localhost:9200/my_index/_search/?q=hello',
  method: 'POST'
}).then(function(body){
  console.log(body);
});

When passing parameters in the body (json), the problems start:

$.ajax({
  url: '//localhost:9200/my_index/_search/',
  method: 'POST',
  dataType: 'json',
  contentType: "application/json",
  data: this.params
}).then(function(body){
  console.log(body);
});

Instead of sending the POST request, the browser (Chrome) sends a OPTIONS request.

Reading jQuery's documentation I see:

For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

So that's probably where it comes from.

The problem is, nothing happens after that preflight request.

Looking at the request itself in Wireshark:

OPTIONS /my_index/_search/ HTTP/1.1
Host: localhost:9200
Connection: keep-alive
Cache-Control: max-age=0
Access-Control-Request-Method: POST
Origin: http://localhost:4000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://localhost:4000/search/?q=hello
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,fr;q=0.6

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: 
Access-Control-Max-Age: 1728000
date: Tue, 12 Apr 2016 07:30:50 GMT
content-length: 0

I noticed that the response's Access-Control-Allow-Methods field is empty, so I am thinking it may be related.

My elasticsearch.yml file contains:

http.cors.enabled: true
http.cors.allow-origin: "*"

I added:

http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE

But it did not change anything.

1 answers

3

Это вызвано этим вопросом , который был зафиксирован и будет выпущен в 2.3.2

Val