javascript - 在 httpd.conf 中启用 CORS

标签 javascript html apache cors httpd.conf

我正在开发一个网络应用程序,它使REST调用其域之外的另一个 Web 应用程序。

但是每当它尝试进行 REST 调用时,我都会面临 CORS Chrome 中的问题。

错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

我知道这个问题已经被问过很多次并且我已经尝试了很多建议,但没有一个对我有用。

我正在使用httpd作为网络服务器。

下面是我系统的httpd.conf。 我添加了Header set Access-Control-Allow-Origin "*"里面<Directory> ,按照建议here .

请提出建议。

谢谢

httpd.conf

ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
    AllowOverride none
  Header set Access-Control-Allow-Origin "*"
    Require all granted
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf

ProxyRequests Off
ProxyPreserveHost On
<VirtualHost *:80>
  ProxyPass / http://localhost:9000/        # My local sonarqube URL
  ProxyPassReverse / http://myhost.com
  ErrorLog logs/sonar/error.log
  CustomLog logs/sonar/access.log common
</VirtualHost>

最佳答案

在基于 Debian 的发行版中。启用 mod_headers 运行

a2enmod headers
sudo service apache2 reload

在 CentOS 和其他基于 RedHat 的发行版中

编辑apache读取的配置文件,如httpd.conf并添加

LoadModule headers_module modules/mod_headers.so

并使用sudo service httpd restart重新加载apache

以及在 httpd.conf 或 apache 读取的某些文件(如 apache2.conf)中,文件 *.conf 位于站点可用/或站点启用/等文件夹中

Header set Access-Control-Allow-Origin: *

* 或您想要的一个或多个域

Keep in mind this must be placed in the server that receive the request, not the server that send the request, and restrictions are made by browsers, not servers, see this: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

还有另一种方法可以代替编辑某些 .conf 文件,即在文档根级别创建一个名为 .htaccess 的文件,并在其中:Header set Access-Control-Allow-Origin *

关于javascript - 在 httpd.conf 中启用 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51506742/

相关文章:

javascript - 找不到变量 : DS in Ember CLI

apache - https 仅适用于 site1,但 site2 不适用于同一台服务器

javascript - 在 Owl Carousel 2 中加载动态内容

javascript - Fabric.js : Change one or multiple selection square/rectangle color

javascript - 将数据从textarea保存到html数据库中

html - css 移动 View 不起作用

Apache Tomcat 配置

apache - 带有磅的 Varnish 和 SSL

javascript - 尝试导入错误: 'react-table' does not contain a default export (imported as 'ReactTable' )

html - Firefox 中的 Helvetica Neue 字体问题