我使用这个脚本来读取xml文件并将其发布到asp.net页面:
<script type="text/javascript">
var doc = "Instructions.xml";
var path = "/pages/Support/Word/";
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", path + doc, false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
document.getElementById("head").innerHTML =
xmlDoc.getElementsByTagName("row")[0].childNodes[0].nodeValue;
document.getElementById("text").innerHTML +=
xmlDoc.getElementsByTagName("line")[0].childNodes[0].nodeValue
</script>
此脚本可在Google Chrome浏览器和Safari中正常使用;但在IE7中,Mozilla根本就没有用......就像那样 有人知道为什么会发生这种情况并给予很大帮助吗?
额外更新
好吧,我多次尝试查看调试器,我发现只出现了一个问题。
当程序到达时document.getElementById("head").innerHTML = xmlDoc.getElementsByTagName("text")[0].childNodes[0].nodeValue;
然后给我一个错误{ {1}}如果那样会有帮助
答案 0 :(得分:0)
这篇文章应该回答你的问题: Easiest way to retrieve cross-browser XmlHttpRequest
它解释了如何迭代不同的解决方案,直到找到适合所涉及浏览器的解决方案。
您可以在脚本标记(现有标记或新标记)中插入答案1中的代码。
要获取/发布某些内容,您将使用链接中提到的函数sendRequest(url,callback,postData)
。它有3个参数:url的字符串,请求执行回调的回调函数,last是boolean(true或false),用于判断函数是应该使用POST(true)还是GET(false)。在你的情况下,它将是GET。记住在出现问题时(代码中的注释)实现一种错误处理方法
总结一下就是这样(记得在链接中添加代码(1.答案)):
<script type="text/javascript">
function performAction()
{
var doc = "Instructions.xml";
var path = "/pages/Support/Word/";
sendRequest(path + doc, requestCallback, false);
}
function requestCallback(xmlHttp)
{
var xmlDoc = xmlHttp.responseXML;
document.getElementById("head").innerHTML = xmlDoc.getElementsByTagName("row")[0].childNodes[0].nodeValue;
document.getElementById("text").innerHTML += xmlDoc.getElementsByTagName("line")[0].childNodes[0].nodeValue;
}
</script>
答案 1 :(得分:0)
最后......经过大量的网上冲浪......我发现了这个问题。 在我的.xml文件中,我有以下结构:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/3/main">
<w:row>
<w:text>
<w:line>
</w:line>
</w:text>
</w:row>
</w:document>
Google和Safari将node name
理解为line
,但其他浏览器则不然。
因此,我更改了node name
并将其设为<text>
或<line>
,然后所有浏览器都能理解相同的问题。
但我觉得这是来自Internet Explorer和Mozilla网站的非常挑衅行为。