我的 "Access-Control-Allow-Origin" header 在我的 .htaccess 中不起作用?

My "Access-Control-Allow-Origin" header is not working in my .htaccess?

当我尝试从“https://api.steampowered.com”API 获取信息时,我遇到了错误

Access to XMLHttpRequest at 'https://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=<<MyKEY>>&steamid=<<MySteamID>>' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

然后我在 apache2.conf 中将 AllowOverride 设置为 all,并将 header Header set Access-Control-Allow-Origin "https://api.steampowered.com" 添加到位于 [=20= 的 .htaccess 中]. (我也用 Header set Access-Control-Allow-Origin "*" 试过了) 我用 a2enmod headers 启用了 a2enmod headers 之后我用 /etc/init.d/apache2 restart 重新启动了我的网络服务器。 但是还是不行。

我的.htaccess:

Header set Access-Control-Allow-Origin "*"

我的apache2.conf:

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted
</Directory>

我的javascript代码:

$(document).ready(function () {
    $.getJSON('https://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=<<MyKEY>>&steamid=<<MySteamID>>', function (data) {
        //My code with the data
    });
});

也许我需要把 header 放在别处?

您有一个网页,https://example.com/,其中包含一些 JavaScript。

JavaScript 想要从 https://api.steampowered.com/ 中读取数据。

为此,https://api.steampowered.com/ 需要授予 https://example.com 权限。

您已配置 https://example.com 授予所有网站从中读取数据的权限。

您尚未配置 https://api.steampowered.com/ 向任何人授予权限。 (至少我假设你不为 Steam 工作)。