Как исправить ошибку CORS HTTPS API на странице - PullRequest
0 голосов
/ 03 июля 2019

У меня есть веб-страница на https://server.mysite.com/level/two

Я обслуживаю конечную точку API в: https://server.mysite.com:17777/twbets

Я получаю сообщение об ошибке:

Access to XMLHttpRequest at 'https://server.mysite.com:17777/twbets' from origin 'https://server.mysite.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Как мне заставить эту ошибку исчезнуть? Я чувствую, что пробовал буквально каждую перестановку для конфигурационного файла nginx и не могу заставить его работать. Ниже приведена часть моего конфигурационного файла, доступного на моих сайтах. Мне не нужны отдельные разделы для каждого типа запроса, это просто самая свежая вещь, которую я пробовал, потому что другой вопрос предложил это.

server {

    server_name server.mysite.com;
    root /var/www;

        location ^~ /level/two {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            #
            #
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
         }
         if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            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';
         }
         if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            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';
         }
    }

...