请求的资源上不存在“Access-Control-Allow-Origin"标头.Origin '...' 因此不允许访问 [英] No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access
问题描述
我使用 .htaccess 来重写 url,我使用了 html base 标签来使它工作.
I'm using .htaccess to rewrite urls and I used html base tag in order to make it work.
现在,当我尝试发出 ajax 请求时,出现以下错误:
Now, when I try to make an ajax request I get the following error:
XMLHttpRequest 无法加载 http://www.example.com/login.php
.请求的资源上不存在Access-Control-Allow-Origin"标头.因此不允许访问源http://example.com
".
XMLHttpRequest cannot load
http://www.example.com/login.php
. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com
' is therefore not allowed access.
推荐答案
使用 addHeader
而不是使用 setHeader
> 方法,
Use addHeader
Instead of using setHeader
method,
response.addHeader("Access-Control-Allow-Origin", "*");
上面一行中的
*
将允许访问所有域
.
*
in above line will allow access to all domains
.
用于允许仅访问特定域
:
For allowing access to specific domain only
:
response.addHeader("Access-Control-Allow-Origin", "http://www.example.com");
检查这个 博文
.
这篇关于请求的资源上不存在“Access-Control-Allow-Origin"标头.Origin '...' 因此不允许访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!