2

I need my application work with backend API on other domain. I use nginx 1.12.1 on Ubuntu 16.04.

I added to my host config following (to the location ~ .php$ section):

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';

Reloaded nginx and tested it with following JS:

var req = new XMLHttpRequest();
req.open('GET', 'http://api.mydomain.com/v1/client', false);
req.send(null);
var headers = req.getAllResponseHeaders();
console.log(headers);

Console log is:

content-type: application/json; charset=UTF-8

That means CORS is enabled, but no other headers received. How to get other headers?

atom-22
  • 121
  • 2
  • 1
    Because you added it to the .php$ section, the CORS headers will only be present when requesting a php file, which your not doing in your AJAX call. – Sage Nov 05 '17 at 20:20

0 Answers0