【问题标题】:CORS header not being served by Apache if there's a 404如果有 404,则 Apache 不提供 CORS 标头
【发布时间】:2023-03-05 13:32:01
【问题描述】:

我在 MAMP PRO 的虚拟主机配置中添加了以下内容:

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

(这是“附加参数”框中唯一的内容)

如果我只是从虚拟主机的域中加载图像来检查标头,效果会很好。

如果我尝试访问导致(正确)404.. 标头不再设置的 URL,则效果不佳。

这正常吗?好像不太正常。为什么 Apache(或 mamp)会因为有 404 而决定忽略我的 'Header set' 指令?

加载图像的请求标头:

Host: redacted.dev
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

加载图像的响应标头:

Accept-Ranges: bytes
Access-Control-Allow-Origin: *
Connection: Keep-Alive
Content-Length: 18575
Content-Type: image/jpeg
Date: Wed, 18 Feb 2015 13:27:25 GMT
Etag: "81-488f-50f376cb56b40"
Keep-Alive: timeout=5, max=100
Last-Modified: Mon, 16 Feb 2015 16:53:25 GMT
Server: Apache

请求标头试图访问不存在的东西:

Host: redacted.dev
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

试图访问不存在的东西的响应标头:

Connection: Keep-Alive
Content-Length: 251
Content-Type: text/html; charset=iso-8859-1
Date: Wed, 18 Feb 2015 13:28:50 GMT
Keep-Alive: timeout=5, max=100
Server: Apache

这个有点失落……

【问题讨论】:

  • 这有什么关系?图像不会以一种或另一种方式加载。
  • 因为最终我使用 ajax 来测试图像是否存在。如果它是 404,那么我在控制台中收到 CORS 错误消息,它确实如此,那么一切都很好。我不希望人们的控制台充满 CORS 错误

标签: apache http cors vhosts


【解决方案1】:

你需要“always”前缀...

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

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 2019-04-08
    • 2015-04-02
    • 2019-02-24
    • 2019-04-24
    • 2019-04-08
    • 2012-06-19
    • 2018-05-25
    • 2020-01-13
    相关资源
    最近更新 更多