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?

  • 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 5 '17 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.