请求在 WAMP 上被阻止,但在 XAMPP 上允许 CORS
Request Blocked on WAMP, but CORS allowed on XAMPP
我尝试使用 WAMP 调用存储在 XAMPP 端口 8012
上的 jpg 文件,从端口 8080
发送 XHR。
我在 XAMPP 服务器上有一个 htaccess
文件,其中包含以下内容,为了允许 CORS:
<IfModule mod_rewrite.c>
Header add Access-Control-Allow-Origin: "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header add Access-Control-Allow-Headers: "Content-Type"
RewriteEngine on
RewriteBase /
</IfModule>
我什至尝试编辑配置文件 httpd.conf
文件 with this guide,但没有任何效果。 WAMP 发送 GET
请求并收到以下错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at http://localhost:8012/cdn_8080/video.jpg.
(Reason: missing token 'access-control-allow-origin' in CORS header
'Access-Control-Allow-Headers' from CORS preflight channel).
我检查过,mod_rewrite
在 xampp 上启用:
LoadModule rewrite_module modules/mod_rewrite.so
我做错了什么?
我解决了我自己的问题。
我的 Javascript 文件包含以下几行:
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,DELETE,PUT');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'Content-Type');
// xhr.withCredentials = true;
// will return an error
这是不必要的,所以我删除了它们,现在一切正常。
我尝试使用 WAMP 调用存储在 XAMPP 端口 8012
上的 jpg 文件,从端口 8080
发送 XHR。
我在 XAMPP 服务器上有一个 htaccess
文件,其中包含以下内容,为了允许 CORS:
<IfModule mod_rewrite.c>
Header add Access-Control-Allow-Origin: "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header add Access-Control-Allow-Headers: "Content-Type"
RewriteEngine on
RewriteBase /
</IfModule>
我什至尝试编辑配置文件 httpd.conf
文件 with this guide,但没有任何效果。 WAMP 发送 GET
请求并收到以下错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8012/cdn_8080/video.jpg. (Reason: missing token 'access-control-allow-origin' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel).
我检查过,mod_rewrite
在 xampp 上启用:
LoadModule rewrite_module modules/mod_rewrite.so
我做错了什么?
我解决了我自己的问题。
我的 Javascript 文件包含以下几行:
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,DELETE,PUT');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'Content-Type');
// xhr.withCredentials = true;
// will return an error
这是不必要的,所以我删除了它们,现在一切正常。