Pregunta

I use this script in order to read from an xml file and post it to a asp.net page:

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

This script works fine in Google Chrome browser, and Safari; but in IE7 and Mozilla simply doesn't work... just like that.
Does someone know why this happens and give a great assistance?

ADDITIONAL UPDATE

Well I tried many times also looking on debugger i saw only one problem comes up.
When the program comes to line
document.getElementById("head").innerHTML = xmlDoc.getElementsByTagName("text")[0].childNodes[0].nodeValue;
then throw me an error of JavaScript runtime error: Unable to get property 'childNodes' of undefined or null reference If that will helps

¿Fue útil?

Solución 2

Finally… And after lot’s of surfing on the net… I discover the problem of this issue. In my .xml file I have the following structure:

<?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 and Safari understands the node name as line but the other browsers don’t. Thus I’ve change the node name and make it <text> or <line> and then all browsers understands the same issue.
But I have the feeling that this is very provocation behavior from the site of Internet Explorer and Mozilla.

Otros consejos

This post should answer you question: Easiest way to retrieve cross-browser XmlHttpRequest

It explains how to iterate through different solutions until it finds one that fits into the browser in question.

You would insert the code from answer 1 in a script tag (existing or a new one).

To get/post something you would use the function sendRequest(url,callback,postData) mentioned in the link. It has 3 parameters: a string for url, a callback function at which the request would perform a callback, and last is boolean (true or false) to tell if the function should use POST (true) or GET (false). In your case it would be GET. Remember to implement a way of error handling in case something goes wrong (the comment in the code)

To sum up it would be something like this (remember to add the code (1. answer) in the link):

<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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top