我正在按照书本示例进行操作,因此代码非常简单。
这是代码:
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 无法加载文件。收到无效响应。因此不允许访问 Origin 'null'。]
然后按照以下步骤创建一个批处理文件:
在桌面中打开记事本。
启动“chrome”“C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”--allow-file-access-from-files 退出
这会做什么?它将打开具有文件访问权限的 Chrome.exe。 现在,从您计算机的任何位置,使用 Google Chrome 浏览您的 html 文件。 我希望这会解决 XMLHttpRequest 问题。
请记住:只需在需要时使用快捷方式 bat 文件打开 Chrome。 告诉我它是否解决了您的问题。我有一个类似的问题,我用这种方式解决了它。谢谢。
关于javascript - 仅 Chrome 错误 : XMLHttpRequest cannot load file URL No 'Access-Control-Allow-Origin' header is present on the requested resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21012385/