Javascript 在Ubuntu 14.04上配置apache 2.4以启用CORS

Javascript 在Ubuntu 14.04上配置apache 2.4以启用CORS,javascript,ajax,cors,ubuntu-14.04,apache2.4,Javascript,Ajax,Cors,Ubuntu 14.04,Apache2.4,我有这个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 JQu

我有这个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>
在web服务器中(Ubuntu 14.04上的Apache 2.4)。。。。我以这种方式修改了apache2.conf文件

<Directory /var/www/cesarefortelegram>
        Order Allow,Deny
        Allow from all
        AllowOverride all
        Header set Access-Control-Allow-Origin "*"
</Directory>
我哪里做错了


提前感谢

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

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


命令允许,拒绝
通融
允许超越所有
标题集访问控制允许原点“*”

现在一切都好了

对我有效的Ubuntu Apache2解决方案 .htaccess edit对我不起作用,我必须修改conf文件

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


ServerName mydomain.xyz
ServerAlias www.mydomain.xyz
服务器管理员support@mydomain.xyz
DocumentRoot/var/www/mydomain.xyz/public
###以下三行用于CORS支持
标题添加访问控制允许源“*”
标题添加访问控制允许标题“来源,x请求,内容类型”
标题添加访问控制允许方法“放置、获取、发布、删除、选项”
ErrorLog${APACHE_LOG_DIR}/error.LOG
CustomLog${APACHE\u LOG\u DIR}/access.LOG组合
SSLCertificateFile/etc/letsencrypt/live/mydomain.xyz/fullchain.pem
SSLCertificateKeyFile/etc/letsencrypt/live/mydomain.xyz/privkey.pem
然后键入以下命令 a2enmod标题

在尝试之前,请确保缓存已清除
<Directory /var/www/cesarefortelegram>
        Order Allow,Deny
        Allow from all
        AllowOverride all
        Header set Access-Control-Allow-Origin "*"
</Directory>
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.
<Directory /var/www/html/cesarefortelegram>
        Order Allow,Deny
        Allow from all
        AllowOverride all
        Header set Access-Control-Allow-Origin "*"
</Directory>
<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>