【问题标题】:Configure apache 2.4 on Ubuntu 14.04 for to enable CORS在 Ubuntu 14.04 上配置 apache 2.4 以启用 CORS
【发布时间】:2018-08-24 18:03:25
【问题描述】:

我有这个 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

在网络服务器(Ubuntu 14.04 上的 Apache 2.4)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>

按照这些说明How to allow Cross domain request in 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.

我哪里做错了?

提前致谢

【问题讨论】:

    标签: javascript ajax cors ubuntu-14.04 apache2.4


    【解决方案1】:

    我发现我的 apache2.conf 文件中有错误....

    这是正确的配置(在“www”之后忘记了“html”......对不起......)......

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

    现在一切正常...

    【讨论】:

      【解决方案2】:

      对我有用的 Ubuntu Apache2 解决方案 .htaccess 编辑对我不起作用,我不得不修改 conf 文件。

      nano /etc/apache2/sites-available/mydomain.xyz.conf

      <IfModule mod_ssl.c>
          <VirtualHost *:443>
      
              ServerName mydomain.xyz
              ServerAlias www.mydomain.xyz
      
              ServerAdmin support@mydomain.xyz
              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 标头

      在尝试之前确保缓存已清除

      【讨论】:

        猜你喜欢
        • 2016-04-05
        • 2014-10-30
        • 2016-02-23
        • 1970-01-01
        • 2018-07-24
        • 1970-01-01
        • 2018-07-20
        • 2016-05-18
        • 1970-01-01
        相关资源
        最近更新 更多