I'm working on an Angular 1.6.3 app which uses the elasticsearch.js library to query an ElasticSearch 5.4 instance running on a remote server. Recently, our app started having problems where it receives several Response for preflight is invalid (redirect)
errors when it initially reaches out to ElasticSearch for data. Currently this problem only happens in Chrome:
Screenshot of request in Developer Tools:
This error seems to happen randomly, and it doesn't occur when a user accesses the app in Chrome's Incognito Mode or another browser like Firefox.
After doing some research, I found out that this OPTIONS
request is a preflight request the browser sends out to ask the server if it's okay to send the actual POST
containing what I want to query ElasticSearch for. The reason a preflight is occurring is because I am trying to do a POST with application/json
as the Content-Type
header, which isn't one of the "standard" values for the Content-Type
header.
My question is - how do I resolve this? I've changed the CORS settings in elasticsearch.yml
to no avail:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: "X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization"
I've also tried changing the elasticsearch.js Content-Type
headers for each ElasticSearch request but each request continues to send application/json
, like the settings don't listen to what I enter:
var client = elasticsearch.Client({
hosts: [{
host: 'es-proxy.company.net',
port: 80,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}]
});
A screenshot of the OPTION
request that fails from Chrome DevTool net-internals event page:
Any help would be appreciated. Thank you.