reactjs - 即使已设置标头,也不存在“Access-Control-Allow-Origin”标头
问题描述
让我先说一下我了解 CORS 的工作原理以及在请求跨站点资源时如何不收到错误消息。
不幸的是,无论出于何种原因,我仍然收到这个。
过程:
- 我在https://ironeko.com/加载我的网站
- 在 Chrome 上,只有 Chrome,我收到以下错误
Access to image at 'https://api.ironeko.com/wp-content/uploads/2019/08/Group-12-3-300x272.png' from origin 'https://ironeko.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
但是,来自同一资源的所有其他图像在第一页加载时都可以正常加载。
这是在我的.htaccess
文件中:
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
<FilesMatch "\.(bmp|cur|gif|ico|jpe?g|png|svgz?|webp)$">
SetEnvIf Origin ":" IS_CORS
Header Set Access-Control-Allow-Origin "*" env=IS_CORS
</FilesMatch>
</IfModule>
</IfModule>
我是从Mozilla那里得到的。
我检查了我在 Apache 中启用的模块:
core_module (static)
so_module (static)
watchdog_module (static)
http_module (static)
log_config_module (static)
logio_module (static)
version_module (static)
unixd_module (static)
access_compat_module (shared)
actions_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
filter_module (shared)
headers_module (shared) <----
http2_module (shared)
mime_module (shared)
mpm_event_module (shared)
negotiation_module (shared)
proxy_module (shared)
proxy_fcgi_module (shared)
proxy_http_module (shared)
reqtimeout_module (shared)
rewrite_module (shared)
setenvif_module (shared) <----
socache_shmcb_module (shared)
ssl_module (shared)
status_module (shared)
(无论如何,即使没有 IfModule,这仍然不起作用)
编辑:我以前认为问题是由我的正常图像请求引起的。但我正在使用react-palette提出次要请求。这就是我认为实际上导致 CORS 错误的原因。
function returnImageColor(src) {
const { data, loading, error } = usePalette(src);
return data;
}
这里发生了什么?为什么这只是发生在 Chrome 上?CORS 标头中是否有错误?
解决方案
推荐阅读
- mysql - 将分组帖子标记为已读或未读 mysql
- r - 结合两个 grobs ,其中一个使用 grid.draw 创建
- angular - 如何根据下拉菜单 1 中选择的内容在下拉菜单 2 中显示来自 API 的数据
- android - 如何在后台重复警报
- jquery - 关闭按钮和动画效果随页面加载,而不是点击加载
- javascript - 捕捉嵌套
- c++ - 将 yuv 4:2:0 文件转换为 Rgb 未获得预期输出
- css - 将具有混合(固定和百分比)值的 CSS 剪辑路径转换为 SVG 剪辑路径
- android - 表情符号颜色 Google Play 商店应用程序
- angular - 以角度将数据附加到FormData