I have Grafana set up in a Docker container (grafana/grafana
image from Docker repo) with port 3000 forwarded to my localhost. My docker-compose.yml
below:
version: '2.1'
services:
grafana:
image: grafana/grafana
ports:
- 3000:3000
Originally I also have link to Graphite and some volumes and environment configuration (GF_SECURITY_ADMIN_PASSWORD
only) but I suppose it does not matter.
I can get a response from Grafana via simple curl
call:
$ curl http://localhost:3000
<a href="/login">Found</a>.
But when I am trying to get it via AJAX call, it gives me a weird result:
$.ajax({url: 'http://localhost:3000', beforeSend: function(xhr, settings) {alert('before setting header'); xhr.setRequestHeader('Access-Control-Allow-Origin', '*'); alert('after setting header');}});
[many JSON fields]
responseText:""
[many JSON fields]
statusText: "error"
[many JSON fields]
Alerts says that header is set to accept requests from any origin.
The same happens (curl works but ajax not) when I am calling Docker container address directly.
What happens in the background? Why the second request does not work? How can I get response from Grafana via AJAX call?
The issue is the by default CORS is not enabled on grafana. A curl request doesn't check for CORS but a browser does. It is what protect one site to call API of other sites.
So your solution would be to put a reverse nginx proxy in front of Grafana. Below is the
docker-compose.yml
for the sameAnd below nginx config will add CORS to it, but it is very open would allow everyone access
Also for test you should not use
Remove that and test and it should work