The following code works fine in firefox but as with many other things, I cant get it working in Internet explorer (any version).
以下代码在Firefox中运行良好,但与许多其他东西一样,我无法在Internet Explorer(任何版本)中使用它。
Can anybody help?
有人可以帮忙吗?
<body>
<script type="text/javascript">
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","messages.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("entry");
for (i=0;i<x.length;i++)
{
document.write("<b>From:</b> ");
document.write(x[i].getElementsByTagName("username")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<b>Date:</b> ");
document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<b>Message:</b> ");
document.write(x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<br />");
}
</script>
</body>
1 个解决方案
#1
1
Two potential issues spring to mind:
我想到两个潜在的问题:
-
Perhaps your AJAX communication is failing. In which case I would strongly urge you to use a library like JQuery to handle your communication. Then you don't have to worry about the browser compatibility issues
也许你的AJAX通信失败了。在这种情况下,我强烈建议您使用像JQuery这样的库来处理您的通信。然后,您不必担心浏览器兼容性问题
-
Secondly, the data coming back from the server is not sent with
content-type("application/xml")
. Different browsers may behave differently if they cannot reliably detect the content type and know that it's XML that you're expecting.其次,从服务器返回的数据不与内容类型(“application / xml”)一起发送。如果不同浏览器无法可靠地检测内容类型并且知道它是您期望的XML,则它们的行为可能会有所不同。
[Edit]
[编辑]
This is a full working example that I tested in IE7
这是我在IE7中测试的完整工作示例
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
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","messages.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("entry");
for (i=0;i<x.length;i++)
{
document.write("<b>From:</b> ");
document.write(x[i].getElementsByTagName("username")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<b>Date:</b> ");
document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<b>Message:</b> ");
document.write(x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<br />");
}
</script>
<div>Hi</div>
</body>
</html>
And the test data from messages.xml
also located at the root of my server
来自messages.xml的测试数据也位于我的服务器的根目录下
<?xml version="1.0"?>
<root>
<entry>
<username>A</username>
<date>B</date>
<message>C</message>
</entry>
</root>
This produces the following output
这会产生以下输出
From: A
Date: B
Message: C
Hi
IE is a pain to debug because it has very limited debugging tools. I checked with FireFox's FireBug add-on to see that the content-type from my WAMP server was "application/xml".
IE很难调试,因为它的调试工具非常有限。我查看了FireFox的FireBug插件,看看我的WAMP服务器中的内容类型是“application / xml”。
You should also make sure that your XML document is valid by running it through a validator.
您还应通过验证程序运行XML文档以确保其有效。
#1
1
Two potential issues spring to mind:
我想到两个潜在的问题:
-
Perhaps your AJAX communication is failing. In which case I would strongly urge you to use a library like JQuery to handle your communication. Then you don't have to worry about the browser compatibility issues
也许你的AJAX通信失败了。在这种情况下,我强烈建议您使用像JQuery这样的库来处理您的通信。然后,您不必担心浏览器兼容性问题
-
Secondly, the data coming back from the server is not sent with
content-type("application/xml")
. Different browsers may behave differently if they cannot reliably detect the content type and know that it's XML that you're expecting.其次,从服务器返回的数据不与内容类型(“application / xml”)一起发送。如果不同浏览器无法可靠地检测内容类型并且知道它是您期望的XML,则它们的行为可能会有所不同。
[Edit]
[编辑]
This is a full working example that I tested in IE7
这是我在IE7中测试的完整工作示例
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
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","messages.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("entry");
for (i=0;i<x.length;i++)
{
document.write("<b>From:</b> ");
document.write(x[i].getElementsByTagName("username")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<b>Date:</b> ");
document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<b>Message:</b> ");
document.write(x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<br />");
}
</script>
<div>Hi</div>
</body>
</html>
And the test data from messages.xml
also located at the root of my server
来自messages.xml的测试数据也位于我的服务器的根目录下
<?xml version="1.0"?>
<root>
<entry>
<username>A</username>
<date>B</date>
<message>C</message>
</entry>
</root>
This produces the following output
这会产生以下输出
From: A
Date: B
Message: C
Hi
IE is a pain to debug because it has very limited debugging tools. I checked with FireFox's FireBug add-on to see that the content-type from my WAMP server was "application/xml".
IE很难调试,因为它的调试工具非常有限。我查看了FireFox的FireBug插件,看看我的WAMP服务器中的内容类型是“application / xml”。
You should also make sure that your XML document is valid by running it through a validator.
您还应通过验证程序运行XML文档以确保其有效。