简体   繁体   English

仅限Chrome中的错误:XMLHttpRequest无法加载文件网址请求的资源上没有“Access-Control-Allow-Origin”标头

[英]Error in Chrome only: XMLHttpRequest cannot load file URL No 'Access-Control-Allow-Origin' header is present on the requested resource

I am following a book example hence the code is very simple. 我正在关注一本书的例子,因此代码非常简单。

This is the code: 这是代码:

jQuery.get("ajax_search_results.php",
           { s:search_query },
           write_results_to_page, "html");

And the code is ajax_search_results.php has: 代码是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>

This works as expected in the Firefox. 这在Firefox中可以正常工作。 But in Chrome, I can see the error following message in the JS console: 但在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.

Can you please help identify the problem? 你能帮忙找出问题所在吗?

If your problem is like the following while using Google Chrome: 如果您在使用Google Chrome时遇到以下问题,请执行以下操作:

[XMLHttpRequest cannot load file. [XMLHttpRequest无法加载文件。 Received an invalid response. 收到无效回复。 Origin 'null' is therefore not allowed access.] 因此不允许原点'null'访问。]

Then create a batch file by following these steps: 然后按照以下步骤创建批处理文件:

Open notepad in Desktop. 在桌面中打开记事本

  1. Just copy and paste the followings in your currently opened notepad file: 只需将以下内容复制并粘贴到当前打开的记事本文件中即可:

start "chrome" "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" --allow-file-access-from-files exit 启动“chrome”“C:\\ Program Files(x86)\\ Google \\ Chrome \\ Application \\ chrome.exe” - allow-file-access-from-files退出

  1. Note: In the previous line, Replace the full absolute address with your location of chrome installation. 注意:在上一行中,将完整的绝对地址替换为chrome安装的位置。 [To find it...Right click your short cut of chrome.exe link or icon and Click on Properties and copy-paste the target link ][Remember : start to files in one line , & exit in another line by pressing enter] [找到它...右键单击chrome.exe链接或图标的快捷方式,然后单击属性并复制粘贴目标链接 ] [记住: 在一行中开始文件,按Enter键退出另一行 ]
  2. Save the file as fileName.bat [Very important: .bat ] 将文件另存为fileName.bat [非常重要: .bat ]
  3. If you want to change the file later then right-click on the .bat file and click on edit. 如果要稍后更改文件,请右键单击.bat文件,然后单击编辑。 After modifying, save the file. 修改后,保存文件。

This will do what? 这会做什么? It will open Chrome.exe with file access. 它将打开具有文件访问权限的Chrome.exe。 Now, from any location in your computer, browse your html files with Google Chrome. 现在,从计算机的任何位置,使用Google Chrome浏览您的html文件。 I hope this will solve the XMLHttpRequest problem. 我希望这将解决XMLHttpRequest问题。

Keep in mind : Just use the shortcut bat file to open Chrome when you require it. 请注意:只需使用快捷方式bat文件即可在需要时打开Chrome。 Tell me if it solves your problem. 告诉我它是否解决了你的问题。 I had a similar problem and I solved it in this way. 我有类似的问题,我以这种方式解决了它。 Thanks. 谢谢。

add this at the top of file, 将其添加到文件顶部,

header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");

This is supposedly because you trying to make cross-domain request, or something that is clarified as it. 这可能是因为您尝试进行跨域请求,或者是为此澄清的内容。 You could try adding header('Access-Control-Allow-Origin: *'); 您可以尝试添加header('Access-Control-Allow-Origin: *'); to the requested file. 到请求的文件。

Also, such problem is sometimes occurs on server-sent events implementation in case of using event-source or XHR polling in IE 8-10 (which confused me first time). 此外,如果在IE 8-10中使用event-sourceXHR polling (这使我第一次困惑),有时会在服务器发送的事件实现上发生此类问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XMLHttpRequest无法加载。 。 。 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 - XMLHttpRequest cannot load . . . No 'Access-Control-Allow-Origin' header is present on the requested resource. XMLHttpRequest无法加载http:// *********。 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - XMLHttpRequest cannot load http://*********. No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载“…所请求的资源上没有&#39;Access-Control-Allow-Origin&#39;头。” — jQuery - XMLHttpRequest cannot load “ …No 'Access-Control-Allow-Origin' header is present on the requested resource.” — Jquery XMLHttpRequest无法加载…所请求的资源上没有“ Access-Control-Allow-Origin”标头 - XMLHttpRequest cannot load…No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载没有&#39;Access-Control-Allow-Origin&#39;&gt;头出现在所请求的资源上 - XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' > header is present on the requested resource 完整日历CakePhp 3.x和错误:XMLHttpRequest无法加载“…”。 请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头吗? - Full Calendar CakePhp 3.x and Error: XMLHttpRequest cannot load '…'. No 'Access-Control-Allow-Origin' header is present on the requested resource? XMLHttpRequest请求的资源上没有“Access-Control-Allow-Origin”标头 - XMLHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载。 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许原点访问 - XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access XHR无法加载 <URL> 。 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - XHR cannot load <URL>. No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin'http:// localhost:3000'Google maps - XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' Google maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM