无法使用Firefox以外的JavaScript读取XML文件 [英] Cannot read XML file using javascript other than Firefox

查看:129
本文介绍了无法使用Firefox以外的JavaScript读取XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if(window.XMLHttpRequest)
我使用下面的代码来读取一个外部的xml文件: {//用于IE7 +,Firefox,Chrome,Opera,Safari的代码
$ b xmlhttp = new XMLHttpRequest();
}
else
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);

xmlhttp.open(GET,myxmlfile.xml,false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;

但是上面的代码似乎只适用于Firefox而不适用于Chrome / IE / Opera。 p>

在Chrome中,由于不允许跨域访问,所以在控制台中出现错误,但是我的xml文件,js文件和html文件全部位于本地硬盘驱动器的相同文件夹中。

对此有何帮助?

谢谢。

解决方案

您可能在本地测试它,而不使用http服务器。

基本上,ajax请求必须使用相同的域,如果您向其他网站发送ajax请求,它将被浏览器阻止(这是一项安全功能,可防止用户从其他网站读取浏览器所有者的数据)。大多数浏览器以相同的方式阻止访问本地文件系统,以防止(例如)恶意电子邮件附件。你会得到一个错误,像 XMLHttpRequest无法加载file:///path/to/your/data.html。如果你在Linux或者Mac上或者安装了python,那么最简单的方法就是使用Access-Control-Allow-Origin。启动一个http服务器是在你的html文件的根目录下使用 python -m SimpleHTTPServer 命令,那么你可以在 http:// localhost:8000 / file.html 如果你在Windows上,那么你可能需要配置IIS(请记住IIS不支持某些文件扩展名,如 .json 默认情况下,你可能需要配置它)。

如果你仍然需要在本地测试,而不必使用http服务器,那么您可以使用 - 允许文件访问从文件运行Chrome(或者更新Chrome exe的快捷方式,或者使用此开关从终端运行Chrome) / p>

I am using the following code to read an external xml file :

if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari

   xmlhttp=new XMLHttpRequest();
   }
 else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 xmlhttp.open("GET","myxmlfile.xml",false);
 xmlhttp.send();
 xmlDoc=xmlhttp.responseXML; 

But the above code seems to work only on Firefox and not on Chrome/IE/Opera.

In Chrome I am getting an error in Console as cross domain access not allowed, but my xml file, js file and html file are all in the same folder in my local hard drive.

Any help on this?

Thanks.

解决方案

You are probably testing it locally without an http server.

Fundamentally ajax requests must be made using the same domain, if you make an ajax request to a different site it will be blocked by the browser (this is a security feature to prevent people from reading the browser's owner's data from other website). Most browsers block access to the local filesystem in the same way to protect from (for example) malicious email attachments. You'll get an error like XMLHttpRequest cannot load file:///path/to/your/data.html. Origin null is not allowed by Access-Control-Allow-Origin.

If you are on Linux or Mac or have python installed the easiest way to start an http server is using the command python -m SimpleHTTPServer in root directory of your html files, then you can view them at http://localhost:8000/file.html if you're on windows then you may need to configure IIS (keep in mind IIS doesn't support certain file extensions like .json by default so you may have to configure it).

If you still want/need to test locally without having to fiddle with an http server, then you can run Chrome with --allow-file-access-from-files (either update the shortcut to the Chrome exe or run Chrome from the terminal with this switch).

这篇关于无法使用Firefox以外的JavaScript读取XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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