title: Response for preflight is invalid (redirect) with AngularJS and ElasticSearch
body: <p>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 <code>Response for preflight is invalid (redirect)</code> errors when it initially reaches out to ElasticSearch for data. Currently this problem only happens in Chrome:</p>
  
  <p><a href="https://i.stack.imgur.com/i42mL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i42mL.png" alt="Response for preflight is invalid (redirect)"></a></p>
  
  <p>Screenshot of request in Developer Tools:<a href="https://i.stack.imgur.com/4rQgE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4rQgE.png" alt="HTTP request in network tab"></a></p>
  
  <p>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.</p>
  
  <p>After doing some research, I found out that this <code>OPTIONS</code> request is a preflight request the browser sends out to ask the server if it's okay to send the actual <code>POST</code> containing what I want to query ElasticSearch for. The reason a preflight is occurring is because I am trying to do a POST with <code>application/json</code> as the <code>Content-Type</code> header, which isn't one of the "standard" values for the <code>Content-Type</code> header.</p>
  
  <p>My question is - how do I resolve this? I've changed the CORS settings in <code>elasticsearch.yml</code> to no avail:</p>
  
  <pre><code>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"
  </code></pre>
  
  <p>I've also tried changing the elasticsearch.js <code>Content-Type</code> headers for each ElasticSearch request but each request continues to send <code>application/json</code>, like the settings don't listen to what I enter:</p>
  
  <pre><code>var client = elasticsearch.Client({
    hosts: [{
      host: 'es-proxy.company.net',
      port: 80,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }]
  });
  </code></pre>
  
  <p>A screenshot of the <code>OPTION</code> request that fails from Chrome DevTool net-internals event page:
  <a href="https://i.stack.imgur.com/6HLpv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6HLpv.png" alt="enter image description here"></a></p>
  
  <p>Any help would be appreciated. Thank you.</p>