仅在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

查看:134
本文介绍了仅在Chrome中出现错误:XMLHttpRequest无法加载文件网址请求的资源上没有“Access-Control-Allow-Origin”标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是代码:



<$> p $ p> 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 =#>第一个结果< / a>< / li>
< li>< a href =#>第二个结果< / a>< / li>
< li>< a href =#>第三个结果< / a>< / li>
< li>< a href =#>更多...< / a>< / li>
< / ul>
< / div>

这可以在Firefox中按预期工作。
但在Chrome中,我可以在JS控制台中看到以下错误消息:

  XMLHttpRequest无法加载
file:/// C:/xampp/xampp/htdocs/snk/ajax_search_results.php?s = keyword。
否请求的资源上存在Access-Control-Allow-Origin标题。
原因'null'因此不被允许访问。

您能否帮忙找出问题所在?

XMLHttpRequest无法加载文件。收到无效回复。因此不允许Origin'null'访问。]然后按照以下步骤创建批处理文件:


$ b 在桌面中打开记事本


  1. 只需将以下内容复制并粘贴到当前打开的记事本file:

开始chromeC:\程序文件(x86)\Google\Chrome\Application\ chrome.exe - 允许文件从文件中获取
exit


  1. 注意:在上一行中,替换您的铬安装位置的完整绝对地址。
    [要找到它...右键单击您的 chrome.exe链接或图标快捷方式,然后单击属性,然后复制粘贴目标链接] [请记住:在一行中开始文件,& 通过按Enter键退出]

  2. 将文件另存为 fileName.bat [非常重要: .bat ]

  3. 如果您想稍后更改文件,请右键单击.bat文件并单击编辑。修改后保存文件。

这会做什么?它将以文件访问的方式打开Chrome.exe。
现在,从您计算机的任何位置,使用Google Chrome浏览您的html文件。
我希望这将解决XMLHttpRequest问题。



请记住:只需使用快捷方式bat文件即可在需要时打开Chrome。
告诉我,如果它解决了你的问题。我遇到了类似的问题,我以这种方式解决了这个问题。谢谢。


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:

<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. But in Chrome, I can see the error following message in the JS console:

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:

[XMLHttpRequest cannot load file. Received an invalid response. Origin 'null' is therefore not allowed access.]

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

  1. Note: In the previous line, Replace the full absolute address with your location of chrome installation. [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]
  2. Save the file as fileName.bat [Very important: .bat]
  3. If you want to change the file later then right-click on the .bat file and click on edit. After modifying, save the file.

This will do what? It will open Chrome.exe with file access. Now, from any location in your computer, browse your html files with Google Chrome. I hope this will solve the XMLHttpRequest problem.

Keep in mind : Just use the shortcut bat file to open Chrome when you require it. Tell me if it solves your problem. I had a similar problem and I solved it in this way. Thanks.

这篇关于仅在Chrome中出现错误:XMLHttpRequest无法加载文件网址请求的资源上没有“Access-Control-Allow-Origin”标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆