Fredo
February 20, 2021, 11:57am
1
Hello.
I am brand new with influxdb. I wanted to write here first before opening an issue in github. Because I think the problem should be with me.
I want to develop a small web app (with React) and looked here first: Getting Started with JavaScript and InfluxDB 2.0
First I thought I’d just start with something simple and query the health (as described in “getting started”. I develop on one machine and my influxdb is on another server. I get a CORS error.
Access to fetch at 'http://192.168.1.148:8086/health' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request does not pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I have already looked on github. I really can’t figure it out.
This should just work, shouldn’t it?
Many thanks for help. Regards.
Franky1
February 22, 2021, 8:59am
2
I assume you know what CORS is?
CORS errors are a common problem when developing frontend applications locally together with APIs.
I guess one of the two possibilities could solve your problem:
1.) Instead of opening the index.html on localhost directly in the browser, also start a simple web server on localhost. For example:
python -m SimpleHTTPServer
Or if you use Visual Studio Code , install the plugin Live Server , start it and open your web app on localhost.
OR
2.) Allow CORS on the server running InfluxDB
Fredo
February 22, 2021, 10:42am
3
Hi Franky.
Many thanks.
I was able to solve this problem with the help of the following post on github:
opened 09:20AM - 08 Oct 20 UTC
closed 07:43AM - 02 Aug 21 UTC
1.x
<!--
Thank you for reporting a bug in InfluxDB.
* Please ask usage questi… ons on the Influx Community site.
* https://community.influxdata.com/
* Please add a :+1: or comment on a similar existing bug report instead of opening a new one.
* https://github.com/influxdata/influxdb/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+is%3Aclosed+sort%3Aupdated-desc+label%3Akind%2Fbug+
* Please check whether the bug can be reproduced with the latest release.
* The fastest way to fix a bug is to open a Pull Request.
* https://github.com/influxdata/influxdb/pulls
-->
__Description:__
I am noticing a strange behaviour when querying InfluxDB 1.8.3 with Flux, from an Angular web app that uses `@influxdata/influxdb-client`. In my particular case, my web app is accessible at `http://localhost:4200` while InfluxDB is hosted on another machine at `http://192.168.225.5:8086`.
The query request fails because of CORS policy, since this is an actual cross-origin request. But should it?
According to issue influxdata/influxdb#18391 and the consequent pull request, CORS should be correctly handled by InfluxDB and I supposedly should not be receiving this error:

In the network tab, this is the situation:

HTTP pre-flight OPTIONS requests look like this:

I also tried reverse-proxying InfluxDB through nginx to bypass CORS using [this configuration](https://enable-cors.org/server_nginx.html): at first it seemed to work (the OPTIONS pre-flight request was successful) but any subsequent POST included duplicate Access-Control-* headers (both those returned by InfluxDB and those added by nginx).
Network tab:

Bypassed pre-flight OPTIONS request:

POST response, including duplicate Access-Control-* headers:

This tells me that CORS is indeed handled correctly by InfluxDB, if not for pre-flight requests. That is, the OPTIONS requests on `/api/v2/query` should not return 405 Method Not Allowed but a 2xx success response including the relevant Access-Control-* headers.
__Steps to reproduce:__
You could try creating an Angular web app using `@influxdata/influxdb-client`, but a simple web page including a script that launches an XMLHttpRequest to InfluxDB `/api/v2/query` should be sufficient to trigger the browser's CORS checks.
__Expected behavior:__
Calls to `/api/v2/query` should respond correctly to OPTIONS requests and not fail CORS policy checks.
__Actual behavior:__
OPTIONS pre-flight request to `/api/v2/query` responds with 405 Method Not Allowed and fails CORS policy checks.
__Environment info:__
* System info: Linux 4.19.15-1.ph3-esx x86_64
* InfluxDB version: 1.8.3, Docker image influxdb:1.8 sha256:35de8cc24dfca8ad80fc93bba1bc7125240b3e27b457a1ef529aac5fb6217eef
* Other relevant environment details: Docker 18.06.1 on VMware PhotonOS 3.0
__Config:__
Only customization is `INFLUXDB_HTTP_FLUX_ENABLED=true` environment variable.
In summary use nginx to allow CORS. Well, not so easy for me… but it worked.
1 Like