# Response for preflight is invalid (redirect) with AngularJS and ElasticSearch

**URL:** <https://discuss.elastic.co/t/response-for-preflight-is-invalid-redirect-with-angularjs-and-elasticsearch/88092>\
**Category:** Elasticsearch\
**Created:** [June 2, 2017, 1:54pm UTC](https://discuss.elastic.co/t/response-for-preflight-is-invalid-redirect-with-angularjs-and-elasticsearch/88092 "2017-06-02T13:54:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cailin](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@cailin](https://discuss.elastic.co/u/cailin)\
**Post date:** [June 2, 2017, 1:54pm UTC](https://discuss.elastic.co/t/response-for-preflight-is-invalid-redirect-with-angularjs-and-elasticsearch/88092/1 "2017-06-02T13:54:10Z")

</div>

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:

[![Response for preflight is invalid (redirect)](https://i.stack.imgur.com/i42mL.png)](https://i.stack.imgur.com/i42mL.png)

Screenshot of request in Developer Tools:[![HTTP request in network tab](https://i.stack.imgur.com/4rQgE.png)](https://i.stack.imgur.com/4rQgE.png)

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:  
[![enter image description here](https://i.stack.imgur.com/6HLpv.png)](https://i.stack.imgur.com/6HLpv.png)

Any help would be appreciated. Thank you.

---

<div class="post-metadata">

**Author:** ![cailin](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@cailin](https://discuss.elastic.co/u/cailin)\
**Post date:** [June 7, 2017, 3:34pm UTC](https://discuss.elastic.co/t/response-for-preflight-is-invalid-redirect-with-angularjs-and-elasticsearch/88092/2 "2017-06-07T15:34:22Z")

</div>

The cause of problem can be seen in the Chrome DevTool net-internals event page near the bottom of the `OPTION` request, where it says `CHROME_EXTENSION_REDIRECTED_REQUEST`. A Chrome extension was randomly redirecting my requests to ElasticSearch, effectively cancelling them. Since it is a Chrome extension that was installed by my company (through an enterprise policy) and thus unremovable, I was able to get the ElasticSearch host added to the extension's whitelist.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 3:34pm UTC](https://discuss.elastic.co/t/response-for-preflight-is-invalid-redirect-with-angularjs-and-elasticsearch/88092/3 "2017-07-05T15:34:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
