如何为 XAMPP 目录中的所有文件启用“Access-Control-Allow-Origin"标头? [英] How to enable 'Access-Control-Allow-Origin' header for all files in a directory of XAMPP?
问题描述
我正在开发一个 HTML5 Javascript 应用程序来从我的本地服务器获取图像,该服务器与应用程序在同一台机器上运行.当我在 Chrome 上运行该应用程序时,我得到了:
I am developing a HTML5 Javascript app to get an image from my local server which runs on the same machine as the app. When I run the app on my Chrome, I got:
从原点访问 'http://localhost/someDIrectory/1.jpg' 处的图像'http://localhost:50000' 已被 CORS 策略阻止:否请求中存在Access-Control-Allow-Origin"标头资源.因此,来源 'http://localhost:50000' 是不允许的访问.
Access to Image at 'http://localhost/someDIrectory/1.jpg' from origin 'http://localhost:50000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:50000' is therefore not allowed access.
从这里的许多问题中,答案指出我需要设置
From many questions here, the answers point out that I need to set
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Origin: *");
在 php 文件中,它应该可以工作,但那是用于 php 文件的......图像或带有图像的目录呢?
in php file and it should work, but that is for php files... What about an image or a directory with images?
我也遇到过如何启用XAMPP上的跨域资源共享?并尝试添加
<IfModule C:xampphtdocssomeDIrectory1.jpg>
Header set Access-Control-Allow-Origin: *
</IfModule>
进入 httpd.conf.我尝试重新启动 XAMPP 并重试上述操作.但是我得到的结果还是一样.
into httpd.conf. I tried restart XAMPP and retried the above. But the result I got is still the same.
如何正确设置 XAMPP 的此类设置?
How to correctly set up such setting for XAMPP?
注意:我使用的是 Construct 2,它基本上是作为 HTML5/Javascript 导出的.我只是使用 Sprite Load From URL 动作.
Note: I am using Construct 2 which is basically exported out as an HTML5/Javascript. I am simply using Sprite Load From URL action.
推荐答案
在您的文件目录中创建一个名为.htaccess"的文件,并将以下内容添加到文件中.
Create a file called ".htaccess" in the directory of your files and add the following to the file.
Header set Access-Control-Allow-Origin "http://localhost:50000/"
这篇关于如何为 XAMPP 目录中的所有文件启用“Access-Control-Allow-Origin"标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!