为什么 CORS 不适用于此配置?
Why CORS isn't working with this configuration?
我的情况喜忧参半:
- Ubuntu 服务器 18.04
- 阿帕奇 2.4.29
- 单声道和 mod_mono 已启用
关于页面的混合情况:
- php 页
- ASMX .NET 网络服务
最后,CDN 上的 WebGL 应用程序。
因此,用户可以在 cdn.mywebsite.com 上获取我的 WebGL 应用程序。 WebGL 应用程序必须与我的 asmx .net 网络服务通信。
但我遇到了经典错误:
..has been blocked by CORS policy: Response to preflight request
doesn't pass access control check: It does not have HTTP ok status.
我添加了:
- 在 .htacces 文件中
<IfModule mod_headers.c>
Header add Access-Control-Allow-Origin "*"
</IfModule>
但也在 web.config 文件中(因为我的网络服务也 运行):
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
</system.webServer>
我还启用了 headers apache mod。
CORS 无法正常工作的原因是什么?
我使用另一个 SO 解决方案解决了问题:
handling CORS preflight request in Apache
通过仔细查看,我发现您可以使用重写来做到这一点,例如:
RewriteEngine 开启
RewriteCond %{REQUEST_METHOD} 选项
重写规则 ^(.*)$ $1 [R=200,L]
(确保启用重写 mod)
那么你应该使用"always set"来设置headers,例如:
Header 总是设置 Access-Control-Allow-Origin "*"
Header 始终设置 Access-Control-Allow-Methods "POST, GET, OPTIONS"
我的情况喜忧参半:
- Ubuntu 服务器 18.04
- 阿帕奇 2.4.29
- 单声道和 mod_mono 已启用
关于页面的混合情况: - php 页 - ASMX .NET 网络服务
最后,CDN 上的 WebGL 应用程序。
因此,用户可以在 cdn.mywebsite.com 上获取我的 WebGL 应用程序。 WebGL 应用程序必须与我的 asmx .net 网络服务通信。
但我遇到了经典错误:
..has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
我添加了: - 在 .htacces 文件中
<IfModule mod_headers.c>
Header add Access-Control-Allow-Origin "*"
</IfModule>
但也在 web.config 文件中(因为我的网络服务也 运行):
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
</system.webServer>
我还启用了 headers apache mod。
CORS 无法正常工作的原因是什么?
我使用另一个 SO 解决方案解决了问题:
handling CORS preflight request in Apache
通过仔细查看,我发现您可以使用重写来做到这一点,例如:
RewriteEngine 开启
RewriteCond %{REQUEST_METHOD} 选项
重写规则 ^(.*)$ $1 [R=200,L]
(确保启用重写 mod)
那么你应该使用"always set"来设置headers,例如:
Header 总是设置 Access-Control-Allow-Origin "*"
Header 始终设置 Access-Control-Allow-Methods "POST, GET, OPTIONS"