![](/img/trans.png)
[英]XMLHttpRequest cannot load . . . No 'Access-Control-Allow-Origin' header is present on the requested resource.
[英]Error in Chrome only: XMLHttpRequest cannot load file URL No 'Access-Control-Allow-Origin' header is present on the requested resource
我正在关注一本书的例子,因此代码非常简单。
这是代码:
jQuery.get("ajax_search_results.php",
{ s:search_query },
write_results_to_page, "html");
代码是ajax_search_results.php:
<div id="ajax_search_results">
<ul>
<li><a href="#">First result</a></li>
<li><a href="#">Second result</a></li>
<li><a href="#">Third result</a></li>
<li><a href="#">More...</a></li>
</ul>
</div>
这在Firefox中可以正常工作。 但在Chrome中,我可以在JS控制台中看到错误消息:
XMLHttpRequest cannot load
file:///C:/xampp/xampp/htdocs/snk/ajax_search_results.php?s=keyword.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access.
你能帮忙找出问题所在吗?
如果您在使用Google Chrome时遇到以下问题,请执行以下操作:
[XMLHttpRequest无法加载文件。 收到无效回复。 因此不允许原点'null'访问。]
然后按照以下步骤创建批处理文件:
在桌面中打开记事本 。
启动“chrome”“C:\\ Program Files(x86)\\ Google \\ Chrome \\ Application \\ chrome.exe” - allow-file-access-from-files退出
这会做什么? 它将打开具有文件访问权限的Chrome.exe。 现在,从计算机的任何位置,使用Google Chrome浏览您的html文件。 我希望这将解决XMLHttpRequest问题。
请注意:只需使用快捷方式bat文件即可在需要时打开Chrome。 告诉我它是否解决了你的问题。 我有类似的问题,我以这种方式解决了它。 谢谢。
将其添加到文件顶部,
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
这可能是因为您尝试进行跨域请求,或者是为此澄清的内容。 您可以尝试添加header('Access-Control-Allow-Origin: *');
到请求的文件。
此外,如果在IE 8-10中使用event-source
或XHR polling
(这使我第一次困惑),有时会在服务器发送的事件实现上发生此类问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.