Настройте apache 2.4 в Ubuntu 14.04, чтобы включить CORS.

У меня есть этот код HTML/Javascript....

<html>
  <head>
    <meta charset='utf-8' />
    <title>Title</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <!-- *** References for JQuery ... -->
    <script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
  </head>
  <body>
    <script>
        var city= "Torino";
        $.ajax({
          url: "http://www.mysite1.org/cesarefortelegram/Telegram/OpenProntoSoccorso/API/getProntoSoccorsoDetailsByMunicipality.php?",
          method: "GET",
          crossDomain: true,
          data: {municipality: city, distance:0}
        })
        .done(function(output) {
            alert("OK!");
        })
        .fail(function() {
          // handle error response
          alert("KO!");
      })
    </script>
  </body>
</html>

... опубликовано здесь ...

http://www.mysite2.com/OpenProntoSoccorso/WebMapping/test2.html

На веб-сервере (Apache 2.4 в Ubuntu 14.04) http://www.mysite1.org/.. .. Я изменил свой файл apache2.conf таким образом ....

<Directory /var/www/cesarefortelegram>
        Order Allow,Deny
        Allow from all
        AllowOverride all
        Header set Access-Control-Allow-Origin "*"
</Directory>

следуя этим инструкциям Как разрешить междоменный запрос в apache2.

У меня все еще есть следующая ошибка в консоли веб-браузера (вкладка «Сеть»..)

Failed to load http://www.mysite1.org/cesarefortelegram/Telegram/OpenProntoSoccorso/API/getProntoSoccorsoDetailsByMunicipality.php?&municipality=Torino&distance=0: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.mysite2.com' is therefore not allowed access.

Где я делаю неправильно?

заранее спасибо


person Cesare    schedule 15.03.2018    source источник


Ответы (2)


Я обнаружил, что в моем файле apache2.conf была ошибка ....

Это правильная конфигурация (забыл "html" после "www".. простите...)...

<Directory /var/www/html/cesarefortelegram>
        Order Allow,Deny
        Allow from all
        AllowOverride all
        Header set Access-Control-Allow-Origin "*"
</Directory>

Все теперь работает нормально...

person Cesare    schedule 15.03.2018

Решение Ubuntu Apache2, которое сработало для меня, редактирование .htaccess у меня не сработало, мне пришлось изменить файл conf.

нано /etc/apache2/сайты-доступны/mydomain.xyz.conf

<IfModule mod_ssl.c>
    <VirtualHost *:443>

        ServerName mydomain.xyz
        ServerAlias www.mydomain.xyz

        ServerAdmin [email protected]
        DocumentRoot /var/www/mydomain.xyz/public

        ### following three lines are for CORS support
        Header add Access-Control-Allow-Origin "*"
        Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
        Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLCertificateFile /etc/letsencrypt/live/mydomain.xyz/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.xyz/privkey.pem

    </VirtualHost>
</IfModule>

затем введите следующую команду

заголовки a2enmod

убедитесь, что кэш очищен, прежде чем пытаться

person Nelles    schedule 18.11.2019