[英]No root node detected when loading XML in IE using Javascript
I am attempting to parse a XML file using Javascript and I'm running into issues on IE7. 我正在尝试使用Javascript解析XML文件,但是在IE7上遇到了问题。
If I have this code: 如果我有此代码:
function LoadXml()
{
var xmlPath = document.getElementById("hsTreeviewXmlPath").value;
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
alert("here1");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
alert("here2");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
alert("here3");
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
alert("here4");
} catch (e) {
xmlhttp=false;
}
}
xmlhttp.open("GET",xmlPath,false);
xmlhttp.send(null);
var xmlDoc = xmlhttp.responseXML;
ParseXml(xmlDoc);
}
function ParseXml(xmlDoc)
{
var root = xmlDoc.documentElement;
alert(root);
for(i=0; i< root.childNodes.length; i++)
{
var node = root.childNodes[i];
if(node.nodeType ==1) //process element nodes (type 1)
{
if(node.childNodes.length > 1)
{
CreateChildren("hsTreeview",node);
}
else
{
AddNode("hsTreeview", node);
}
}
}
}
In FF and Chrome this works correctly, adding nodes as it should, but on IE7 I get a scripting error and the specific error: 在FF和Chrome中,这可以正常工作,并按需添加节点,但是在IE7上,我收到脚本错误和特定错误:
Object required
所需对象
This gives a line number relating to the line: 这给出了与该行有关的行号:
for(i=0; i< root.childNodes.length; i++)
{
The alert box tells me that in IE, the root node, which is being populated from xmlDoc.documentElement
is null. 警报框告诉我,在IE中,从
xmlDoc.documentElement
填充的根节点为null。
I have confirmed, using the alerts here1 etc that in IE7 it is using the xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
我已经确认,在IE1中使用警报here1等,它正在使用
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
object. 宾语。
Is there some way to fix this up as it's really frustrating? 有什么办法可以解决这个问题,因为它确实令人沮丧?
Make sure that the XML file is served with the proper text/xml Mime Type. 确保XML文件带有正确的text / xml Mime类型。
Edit Bellow: 编辑波纹管:
Also make sure that the XML file is served through an http server from the same domain and port as the web page. 还要确保XML文件是通过与网页相同的域和端口通过http服务器提供的。 IE will prevent a web page from accessing URLs outside their originating domain or files on your local computer for security reasons.
出于安全原因,IE将阻止网页访问其原始域之外的URL或本地计算机上的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.