![](/img/trans.png)
[英]No Access-Control-Allow-Origin header is present on the requested resource .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 set Access-Control-Allow-Origin "https://api.steampowered.com"
到我位於/var/www/html
.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
});
});
也許我需要把標題放在別處?
您有一個網頁https://example.com/
,其中包含一些 JavaScript。
JavaScript 想要從https://api.steampowered.com/
讀取數據。
要做到這一點, https://api.steampowered.com/
需要給權限https://example.com
。
您已將https://example.com
配置為授予所有網站從中讀取數據的權限。
您尚未將https://api.steampowered.com/
配置為向任何人授予權限。 (至少我假設你不為 Steam 工作)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.