简体   繁体   English

如何为服务器上的图像设置Access-Control-Allow-Origin HTTP标头?

[英]How to set Access-Control-Allow-Origin HTTP header for images on my server?

I'm trying to use my application to load images from my server, but I couldn't do it, as I do not know where to set HTTP header: Access-Control-Allow-Origin: * for images. 我正在尝试使用我的应用程序从服务器加载图像,但是我无法做到,因为我不知道在哪里设置HTTP标头:Access-Control-Allow-Origin:*用于图像。 I have only basic knowledge on web programming, so its quite confusing what I get from the web.. Help is very much appreciated! 我只有Web编程的基本知识,因此我从Web上获得的信息非常令人困惑。非常感谢您的帮助!

You generally don't need this header for images, only for data that's accessed using AJAX. 通常,只需要使用AJAX访问的数据就不需要此标头用于图像。 But if you need it, the proper way is with the header() function: 但是,如果需要,正确的方法是使用header()函数:

header('Access-Control-Allow-Origin: *');

This has to be done before your script produces any output, so it should generally be at the beginning of the script. 必须在脚本产生任何输出之前完成此操作,因此通常应在脚本的开头。

The answer was to include a file named .htaccess (notice the leading dot) anywhere in the path of the requested file, that file should contain this line: 答案是在请求文件的路径中的任何位置都包含一个名为.htaccess的文件(注意前导点),该文件应包含以下行:

Header set Access-Control-Allow-Origin "*" 标头集访问控制允许来源“ *”

The .htaccess can be place anywhere you like as long as its under /public_html .htaccess可以放在您喜欢的任何位置,只要在/ public_html下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM